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>
using namespace std;

#define FOR(i,l,r) for(int i = (l); i <= (r); i++)
#define FORD(i,l,r) for(int i = (l); i >= (r); i--)

using num = double;
using ind = long long;

struct bi{
    ind x;
    ind y;
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ind n,m,s;
    cin >> n >> m >> s;
    vector<bi> v;
    v.push_back({-1,-1});
    FOR(i,1,m){
        v.push_back({0,0});
        cin >> v.back().x;
        cin >> v.back().y;
    }
    sort(v.begin(),v.end(),[](bi a, bi b){
        return a.x < b.x;
    });
    ind last_empty=-1;
    if(v[1].x > 1)last_empty=v[1].x-1;
    FOR(i,1,m){
        if(v[i].x <= s and s <= v[i].y){
            break;
        }
        if(v[i].y + 1 != v[i+1].x){
            last_empty = v[i+1].x-1;
        }
    }
    ind last_empty2=-1;
    if(v[m].y < n)last_empty2=v[m].y+1;
    FORD(i,m,1){
        if(v[i].x <= s and s <= v[i].y){
            break;
        }
        if(v[i].x - 1 != v[i-1].y){
            last_empty2 = v[i-1].y+1;
        }
    }
    if(last_empty == -1){
        cout << last_empty2 << '\n';
        return 0;
    }
    if(last_empty2 == -1){
        cout << last_empty << '\n';
        return 0;
    }
    if(s-last_empty <= last_empty2-s){
        cout << last_empty << '\n';
    }else{
        cout << last_empty2 << '\n';
    }
}