1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
const int N=1e3+5;
LL n,s;
int m;
pair<LL,LL>A[N];
signed main(){
    cin.tie(0)->sync_with_stdio(0);
	cin>>n>>m>>s;
	for(int i=1;i<=m;++i){
		LL l,r;cin>>l>>r;
		A[i]=make_pair(l,r);
	}
	A[m+1]=make_pair(n+1,n+1);
	sort(A+1,A+m+1);
	LL L=0,R=n+1;
	for(int i=1;i<=m+1;++i)
		if(A[i-1].second+1<A[i].first)
			if(A[i].first<=s)
				L=max(L,A[i].first-1);
			else
				R=min(R,A[i-1].second+1);
	if(R>n)cout<<L<<'\n';
	else if(L<1)cout<<R<<'\n';
	else if(s-L<=R-s)cout<<L<<'\n';
	else cout<<R<<'\n';
    return 0;
}