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
import numpy as np
import sys

def best_place_school(array, n, m, s):

    best_r, best_l = np.inf, np.inf
    l = s - 1
    r = s + 1
    found = False
    while found == False:
        l_good = True
        r_good = True
        for i in array:
            if i[0] <= l <= i[1]:
                l_good = False
                l -= 1
            
            if i[0] <= r <= i[1]:
                r += 1 
                r_good = False

        if l_good == True:
            found = True
            best_l = l

        if r_good == True:
            found = True
            best_r = r

    print(min(best_r, best_l))

def read_input():
    lines = sys.stdin.read().splitlines()
    
    n, m, s = map(int, lines[0].split())
    array = [list(map(int, line.split())) for line in lines[1:]]
    
    return n, m, s, array

if __name__ == "__main__":
    n, m, s, array = read_input()
    best_place_school(array, n,m, s)