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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <deque>
#include <tuple>
#include <bitset>

using namespace std;

#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define x first
#define y second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    ll n, m, s;
    cin >> n >> m >> s;

    vector<pll> lower;
    vector<pll> higher;

    ll l_ = s-1;
    ll h_ = s+1;

    for(int i=0; i<m; i++){
        ll l, r;
        cin >> l >> r;
        if(r<=s){
            lower.pb(mp(l, r));
        }else if(l>s){
            higher.pb(mp(l, r));
        }else{
            l_ = l-1;
            h_ = r+1;
        }
    }

    sort(lower.begin(), lower.end());
    sort(higher.begin(), higher.end());

    for(int i = lower.size()-1; i>=0; i--){
        if(lower[i].nd>=l_){
            l_ = lower[i].st -1;
        }else{
            break;
        }
    }


    for(int i = 0; i<higher.size(); i++){
        if(higher[i].st<=h_){
            h_ = higher[i].nd +1;
        }else{
            break;
        }
    }

    ll diff_l = s-l_;
    ll diff_h = h_-s;
    ll ans;

    if(l_==0){
        ans = h_;
    }else if(h_==n+1){
        ans = l_;
    }else if(diff_l <= diff_h){
        ans = l_;
    }else{
        ans = h_;
    }

    cout << ans << "\n";
    return 0;
}