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
n, m, s = map(int, input().split(' '))

a = []
iss = False
for _ in range(m):
    p, q = map(int, input().split(' '))
    a += [(p, q)]

    iss |= p <= s and s <= q

if not iss:
    a += [(s, s)]
    m += 1

a.sort()

best = int(1e16)
best_p = -1

i = 0
while i < m:
    p = a[i][0]
    while i < m - 1 and a[i + 1][0] - a[i][1] == 1:
        i += 1
    q = a[i][1]

    if p - 1 >= 1 and abs(s - (p - 1)) < best:
        best = abs(s - (p - 1))
        best_p = p - 1

    if q + 1 <= n and abs(s - (q + 1)) < best:
        best = abs(s - (q + 1))
        best_p = q + 1

    i += 1

print(best_p)