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

#define all(a) begin(a), end(a)
using ll = long long;

void solve() {
    ll n, m, s;
    cin >> n >> m >> s;

    vector<pair<ll, ll>> a(m);
    for (auto &[l, r] : a)
        cin >> l >> r;

    set<int> bounds;
    for (auto [l, r] : a) {
        bounds.insert(l);
        bounds.insert(r);
    }

    pair<ll, ll> ans{n + 1, n + 1};
    auto update_ans = [&](ll x) {
        if (x <= 0 || x > n || bounds.count(x))
            return;

        ans = min(ans, {abs(x - s), x});
    };

    bool g = true;
    for (auto [l, r] : a) {
        update_ans(l - 1);
        update_ans(r + 1);
        g &= s < l || s > r;
    }

    update_ans(1);
    update_ans(n);

    if (g)
        update_ans(s);

    cout << ans.second << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int tests = 1;
    // cin >> tests;

    while (tests--)
        solve();
}