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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second

const ll INF = 2e12;

int main(){
	//ios_base::sync_with_stdio(0);cin.tie();
	ll n,m,s; cin >> n >> m >> s;

	ll a,b;
	vector<pll> paryprzed;
	vector<pll> parypo;
	for(int x=0;x<m;x++){
		cin >> a >> b;
		if(b<s){
			paryprzed.push_back({a,b});
		}
		else if(a>s){
			parypo.push_back({a,b});
		}
		else{
			paryprzed.push_back({a,s});
			parypo.push_back({s,b});
		}
	}
	paryprzed.push_back({0ll,0ll});		parypo.push_back({n+1,n+1});
	sort(paryprzed.begin(),paryprzed.end()); sort(parypo.begin(),parypo.end());
	reverse(paryprzed.begin(),paryprzed.end());

	/*cout << "MEOW\n";
	for(auto t : paryprzed){
		cout << t.first << ' ' << t.second << '\n';
	}
	cout << '\n';
	for(auto t : parypo){
		cout << t.first << ' ' << t.second << '\n';
	}
	cout << '\n';*/

	ll lewobest=INF,prawobest=INF;
	for(int x=1;x<paryprzed.size();x++){
		ll t = paryprzed[x-1].fi;
		if(t-1>paryprzed[x].se){
			lewobest=t-1;
			break;
		}
	}
	for(int x=1;x<parypo.size();x++){
		ll t = parypo[x-1].se;
		if(t+1<parypo[x].fi){
			prawobest=t+1;
			break;
		}
	}

	//cout << lewobest << ' ' << prawobest << '\n';

	if(lewobest==INF){
		cout << prawobest; return 0;
	}
	if(prawobest==INF){
		cout << lewobest; return 0;
	}
	if(s-lewobest <= prawobest-s){
		cout << lewobest;
	}
	else{
		cout << prawobest;
	}
}