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

const int N = 2e5+1;
const int mod = 1e9+7;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int _;_ = 1;//cin >> _;
    while (_--) {
        int n, m, s;
        cin >> n >> m >> s;
        vector<pair<int,int> > a(m);
        for(int i = 0; i < m; i++) cin >> a[i].first >> a[i].second;
        int pos = s - 1;
        while(true)
        {
            bool ok = false;
            for(int i = 0; i < m; i++) if (pos >= a[i].first && pos <= a[i].second) pos = a[i].first - 1, ok = true;
            if(!ok) break;
        }
    
        int ans = s - pos;
        if(pos == 0) ans = 1e18;
        pos = s + 1; 
        while(true)
        {
            bool ok = false;
            for(int i = 0; i < m; i++) if(pos >= a[i].first && pos <= a[i].second) pos = a[i].second + 1, ok = true;
            if(!ok) break;
        }

        if(ans > pos - s && pos <= n)
        cout << pos << "\n";
        else cout << s - ans << "\n";
    }
}