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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll n, s;
    int m;
    cin >> n >> m >> s;
    
    vector<ll> v(2*m);
    for(int i = 0; i < 2*m; i++) cin >> v[i];
    sort(v.begin(), v.end());
    
    ll pocz = -1;
    for(int i = 0; i < 2*m; i += 2){
        if(pocz == -1) pocz = v[i];
        if(i+2 < 2*m && v[i+2]-v[i+1] == 1) continue;
        if(s <= v[i+1]){
            ll ans = LLONG_MAX;
            ll where = -1;
            if(pocz != 1) if(ans > abs(pocz-1-s)) ans = abs(pocz-1-s), where = pocz-1;
            if(v[i+1] != n) if(ans > abs(v[i+1]+1-s)) where = v[i+1]+1;
            cout << where << '\n';
            return 0;
        }
        pocz = -1;
    }
}