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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;

const int mod = 998244353;

int add(int a, int b)
{
	return a + b < mod ? a + b : a + b - mod;
}

int sub(int a, int b)
{
	return a - b >= 0 ? a - b : a - b + mod;
}

int mult(int a, int b)
{
	return (LL)a * b % mod;
}

int binpow(int a, LL n)
{
	int res = 1;
	while (n)
	{
		if (n & 1)
			res = mult(res, a);
		a = mult(a, a);
		n /= 2;
	}
	return res;
}

template<typename T>
void updMin(T& a, T b)
{
	a = min(a, b);
}

template<typename T>
void updMax(T& a, T b)
{
	a = max(a, b);
}

int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	LL n, s;
	int m;
	cin >> n >> m >> s;
	vector<PLL> segments(m);
	for (auto& [l, r] : segments)
		cin >> l >> r;
	sort(ALL(segments));
	int p = 0;
	while (segments[p].S < s)
		p++;
	assert(segments[p].F <= s && s <= segments[p].S);
	int i = p, j = p;
	while (i > 0 && segments[i - 1].S == segments[i].F - 1)
		i--;
	LL ans1 = segments[i].F - 1;
	while (j + 1 < m && segments[j + 1].F == segments[j].S + 1)
		j++;
	LL ans2 = segments[j].S + 1;
	if (ans1 >= 1 && (ans2 > n || s - ans1 <= ans2 - s)) 
	{
		cout << ans1 << "\n";
	}
	else
	{
		assert(ans2 <= n);
		cout << ans2 << "\n";
	}
	return 0;
}