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
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
using namespace std;
using   ll         = long long;
using   vi         = vector<int>;
using   pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

void run();

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(12);
	run();
	cout << flush; _Exit(0);
}

void run() {
	ll n, s;
	int m;
	cin >> n >> m >> s;
	vector<pair<ll, ll>> segs(m);
	each(e, segs) cin >> e.x >> e.y;
	ll begin = s, end = s;
	while (true) {
		bool ok = 1;
		each(e, segs) {
			if (e.x <= begin && begin <= e.y) {
				begin = e.x-1;
				ok = 0;
			}
			if (e.x <= end && end <= e.y) {
				end = e.y+1;
				ok = 0;
			}
		}
		if (ok) {
			pair<ll, ll> ans = {INT64_MAX, -1};
			if (begin >= 1) ans = min(ans, make_pair(s-begin, begin));
			if (end <= n) ans = min(ans, make_pair(end-s, end));
			cout << ans.y << '\n';
			return;
		}
	}
}