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
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i=(a); i<(b); i++)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif

const int maxM = 1 << 10;

std::pair <long long, long long> T[maxM];

void solve()
{
	long long n, s, a = 0, b = 0;
	int m;
	scanf ("%lld%d%lld", &n, &m, &s);
	FOR(i, 0, m)
	{
		scanf ("%lld%lld", &T[i].first, &T[i].second);
		if (T[i].first <= s and s <= T[i].second)
			std::tie(a, b) = T[i];
	}

	FOR(_, 1, m)
	{
		FOR(i, 0, m)
		{
			if (T[i].second + 1 == a)
				a = T[i].first;
			if (b + 1 == T[i].first)
				b = T[i].second;
		}
	}

	long long res = (s - a <= b - s and a > 1) or b == n
		? a - 1
		: b + 1;

	printf("%lld\n", res);	
}

int main()
{
	int tc = 1;
//	scanf ("%d", &tc);	
	FOR(cid, 1, tc+1)
		solve();
	return 0;
}