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

#define all(x) begin(x),end(x)
#define pb push_back
#define st first
#define nd second

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

const int INF = 2e9;
const ll LLINF = 2e18;

void solve()
{
    ll n,s;
    int m;
    cin>>n>>m>>s;
    vector<pll> v(m);
    for(auto& p:v)
        cin>>p.st>>p.nd;
    sort(all(v));

    int j=0;
    while(v[j].nd<s)
        ++j;

    int l=j;
    while(l>0 && v[l-1].nd==v[l].st-1)
        --l;
    ll p=v[l].st-1;
    if(p==0)
        p=-1e13;

    int r=j;
    while(r<m-1 && v[r].nd+1==v[r+1].st)
        ++r;
    ll q=v[r].nd+1;
    if(q==n+1)
        q=1e13;

    if(q-s<s-p)
        cout<<q<<endl;
    else
        cout<<p<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int z=1;
    //cin>>z;
    while(z--)
        solve();
    return 0;
}