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
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define piii pair<pair<int,int>, int>
#define st first.first
#define nd first.second
#define rd second
#define For(i, l, r) for (int i = l; i <= r; i++)
#define Forcin(l, r, a)        \
  for (int i = l; i <= r; i++) \
    cin >> a[i];
#define Ford(i, l, r) for (int i = l; i >= r; i--)
#define ben(v) v.begin(), v.end()
#define LOCAL 0
#define LOCAL2 0
using namespace std;
 
const int M = 400005, inf=1e14;
 
signed main()
{
  //cin.tie(0)->sync_with_stdio();
    if (LOCAL)
        freopen("a.txt", "r", stdin);
    if (LOCAL2)
        freopen("local_out.txt", "w", stdout);
    int n,m,s;
    cin>>n>>m>>s;
    set<pii> r;
    r.insert({-1, -1});
    r.insert({n+2, n+2});
    For(i, 1, m){
        int a, b;
        cin>>a>>b;
        r.insert({a, b});
    }
    auto it = r.upper_bound({s, inf});
    it--;
    auto it2=it;
    int s1, s2;
    while(true){
        auto it3=it;
        it3--;
        if (it3->second+1<it->first){
            s1=it->first-1;
            break;
        }
        it--;
    }
    while(true){
        auto it3=it2;
        it3++;
        if (it3->first-1>it2->second){
            s2=it2->second+1;
            break;
        }
        it2++;
    }
    if (s1==0)
        s1=-inf;
    if(s2==n+1)
        s2=inf;
    if (s-s1<=s2-s)
        cout<<s1;
    else
        cout << s2;
}