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

typedef long long ll;

#define pb push_back
#define pii pair<ll, ll>
#define st first
#define nd second

const ll MIN = ll(1000 * 1000) * ll(1000 * 1000) * ll(1000);

vector<pii> v;
int m;

bool czyzajety (ll x) {
    for (int i = 0; i < m; i ++) {
        if (v[i].st <= x && x <= v[i].nd) {
            return true;
        }
    }
    return false;
}

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

    ll n, s, l, r;

    cin >> n >> m >> s;

    for (int i = 0; i < m; i ++) {
        cin >> l >> r;
        v.pb({l, r});
    }

    sort(v.begin(), v.end());

    int ind = 0;
    for (int i = 0; i < m; i ++) {
        if (v[i].st <= s && s <= v[i].nd) {
            ind = i;
            break;
        }
    }

    int ind2 = ind;
    ll wyn = -MIN;
    while (ind2 >= 0) {
        if (v[ind2].st - 1LL >= 1LL && !czyzajety(v[ind2].st - 1LL)) {
            wyn = v[ind2].st - 1LL;
            break;
        }
        ind2 --;
    }

    while (ind < m) {
        if (v[ind].nd + 1LL <= n && !czyzajety(v[ind].nd + 1LL)) {
            if (s - wyn > v[ind].nd + 1LL - s) {
                wyn = v[ind].nd + 1LL;
            }
            break;
        }
        ind ++;
    }

    cout << wyn << "\n";

    return 0;
}