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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <algorithm>
#include <iostream>
#include <iso646.h>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <chrono>
#include <limits>
#include <random>
#include <string>
#include <time.h>
#include <thread>
#include <vector>
#include <cmath>
#include <queue>
#include <tuple>
#include <set>
#include <map>

using namespace std;

int main()
{
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);

long long n_build, n_inter, school_index, left, right, help_st, help_end;

cin >> n_build >> n_inter >> school_index;

map<long long, long long> st, end;

for(int i = 0; i < n_inter; i++)
{
    cin >> left >> right;
    st[left] = right;
    end[right] = left;
    if(left <= school_index && right >= school_index)
    {
        help_st = left;
        help_end = right;
    }
}

while(st[help_st])
{
    help_st = st[help_st] + 1;
}

while(end[help_end])
{
    help_end = end[help_end] - 1;
}

if(!help_end)
{
    cout << help_st;
}
else if(help_st > n_build)
{
    cout << help_end;
}
else if(school_index - help_end <= help_st - school_index)
{
    cout << help_end;
}
else
{
    cout << help_st;
}

return 0;
}
/*
15 4 9
4 5
10 13
1 1
6 9

19 6 7
5 7
1 4
9 10
13 14
16 17  
18 19

19 6 2
5 7
1 4
9 10
13 14
16 17  
18 19
*/