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
#include <bits/stdc++.h>
using namespace std;
long n, m, s, res = 10e13;
vector < pair <long, long > > v;
void mn(long i) {
    if (abs(s - i) < abs(s - res)) res = i;
}
void findbest(long a, long b) {
    if (a > s)
        mn(a);
    else if (b >= s)
        mn(s - 1);
    else
        mn(b);
}
int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    cin >> n >> m >> s;
    v.resize(m);
    for (long i = 0; i < m; ++i) {
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    long lastb = 0;
    for (auto & [a, b] : v) {
        if (lastb + 1 < a) {
            findbest(lastb + 1, a - 1);
        }
        lastb = b;
    }
    if (v[m - 1].second < n) findbest(v[m - 1].second + 1, n);
    cout << res;
}