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
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define st first
#define nd second

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll n, m, s;
    cin >> n >> m >> s;
    pair<ll, ll> T[m];
    rep(i, m) {
        cin >> T[i].st >> T[i].nd;
    }
    sort(T, T + m);
    vector<pair<ll, ll>> v;
    ll poc = T[0].st;
    ll kon = T[0].nd;
    for (int i = 1; i < m; i++) {
        if (T[i].st <= kon + 1) {
            kon = max(kon, T[i].nd);
        }
        else {
            v.pb({poc, kon});
            poc = T[i].st;
            kon = T[i].nd;
        }
    }
    v.pb({poc, kon});
    ll ans = 1e18;
    ll kt = -1;
    for (auto p: v) {
        if (p.st <= s && s <= p.nd) {
            if (p.st - 1 > 0) {
                if (s - (p.st - 1) < ans) {
                    ans = s - (p.st - 1);
                    kt = p.st - 1;
                }
            }
            if (p.nd + 1 <= n) {
                if (p.nd + 1 - s < ans) { 
                    ans = p.nd + 1 - s;
                    kt = p.nd + 1;
                }
            }
        }
    }
    cout << kt << '\n';
    return 0;
}