1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def fun():
    n, m, s = map(int, input().split())
    arr = ["0"] * n

    for i in range(int(m)):
        a, b = map(int, input().split())
        arr[a - 1 : b] = ["1"] * (b - a + 1)

    arr[s - 1] = "S"

    left = s - 1
    right = s - 1
    while True:
        if left >= 0 and arr[left] == "0":
            return left + 1
        if right < n and arr[right] == "0":
            return right + 1
        left -= 1
        right += 1

print(fun())