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
def main(listawyj,ranges,school,housenum):


    listawyj.sort(key=lambda i: abs(min(i)-school))
    point1 = max(0,school-1)
    point2 = min(school+1,housenum)
    for elem in listawyj:
        if point1 <= elem[1] and point1 >= elem[0]:
            point1 = elem[0]-1
        if point2 <= elem[1] and point2 >= elem[0]:
            point2 = elem[1]+1
    wyntop = abs(school-point2)
    wynbor = abs(school-point1)
    if wyntop < wynbor:
        if point2 <= housenum:
            return point2
        else:
            return point1
    if wynbor < wyntop:
        if point1 >= 1:
            return point1
        else:
            return point2
    if wyntop == wynbor:
        if point1 >= 1:
            return point1
        return point2



if __name__ == "__main__":
    a = input().split()
    a = list(map(int, a))
    housenum1= a[0]
    ranges1 = a[1]
    school1 = a[2]
    listawyj1 = []
    for elem in range(ranges1):
        przedzial = list(map(int, input().split()))
        listawyj1.append((przedzial[0],przedzial[1]))
    print(main(listawyj1,ranges1,school1,housenum1))