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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e18+7;
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	ll n, m, s;
	cin >> n >> m >> s;
	vector<pair<ll,ll>>T(m);
	rep(i, m) cin >> T[i].st >> T[i].nd;
	sort(all(T));
	vector<pair<ll,ll>>P;
	ll lst=0;
	for(auto i : T) {
		if(lst+1<=i.st-1) P.pb({lst+1, i.st-1});
		lst=i.nd;
	}
	if(lst+1<=n) P.pb({lst+1, n});
	pair<ll,ll>mi={INF, INF};
	for(auto i : P) {
		mi=min(mi, {abs(i.st-s), i.st});
		mi=min(mi, {abs(i.nd-s), i.nd});
	}
	cout << mi.nd << '\n';
}