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
#include<bits/stdc++.h>
#define st first
#define nd second
#define all(x) x.begin(), x.end()
#define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false);
 
// #define int ll
typedef long long ll;

using namespace std;
template <typename T> struct tag:reference_wrapper <T>{ using reference_wrapper <T>::reference_wrapper; };
template <typename T1, typename T2> static inline tag <ostream> operator<<(tag <ostream> os, pair<T1, T2> const& p){ return os.get()<<"{"<<p.first<<", "<<p.second<<"}", os;}
template <typename Other> static inline tag <ostream> operator<<(tag <ostream> os, Other const& o){ os.get()<<o; return os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, vector <T> const& v){ os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, set <T> const& s){ vector <T> v; for (auto i: s) v.push_back(i); os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }



int32_t main(){
    BOOST;
    long long n, m, s; cin >> n >> m >> s;
    vector<pair<long long, long long>> taken(m + 2);
    taken[0] = {-1, -1};
    for(int i = 1; i <= m; i++){
        cin >> taken[i].first >> taken[i].second;
    }
    taken[m + 1] = {n + 2, n + 2};

    sort(all(taken));
    // cout << taken << "\n";
    vector<long long> free;
    long long l, r;
    for(int i = 1; i <= m + 1; i++){
        l = taken[i - 1].second + 1;
        r = taken[i].first - 1;
        if(l > r) continue;
        if(l <= s && s <= r){
            if(l == r) continue;
            if(l <= s - 1) free.push_back(s - 1);
            if(s + 1 <= r) free.push_back(s + 1);
        } else {
            free.push_back(l);
            if(l == r) continue;
            free.push_back(r);
        }
    }
    int idx2 = (upper_bound(all(free), s) - free.begin());
    int idx1 = idx2 - 1;
    if(idx1 == 0){
        cout << free[idx2] << "\n";
        return 0;
    }
    if(idx2 == free.size() - 1){
        cout << free[idx1] << "\n";
        return 0;
    }
    if(abs(s - free[idx1]) <= abs(s - free[idx2])){
        cout << free[idx1] << "\n";
    } else {
        cout << free[idx2] << "\n";
    }
    return 0;
}