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
//
//  main.cpp
//  Szkoła
//
//  Created by dzmitry kavaliou on 11/03/2025.
//


#include <iostream>
#include <tuple>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main(int argc, const char * argv[]) {
    unsigned long long int a, b, c, bestl=1000000000000, bestr=0;
    cin >> a >> b >> c;
    vector<pair<unsigned long long int, unsigned long long>> doit1(b);
    for(int i = 0; i < b; i++){
        cin >> doit1[i].first >> doit1[i].second;
    }
    sort(doit1.begin(), doit1.end());
    for(int i = 0; i < b; i++){
        unsigned long long int l = doit1[i].first, r = doit1[i].second;
        if(c <= r && c >= l){
            if(r<a){
                bestr = r+1;
            }
            else bestr = 100000000000000;
        }
        else if(bestr >= l){
            if(r<a){
                bestr = max(r+1, bestr);
            }
            else bestr = 100000000000000;
        }
    }
    for(int i = b-1; i > -1; i--){
        unsigned long long int l = doit1[i].first, r = doit1[i].second;
        if(c <= r && c >= l){
            if(l>1){
                bestl = l-1;
            }
            else bestl = 100000000000000;
        }
        else if(bestl <= r){
            if(l>1){
                bestl = min(l-1, bestl);
            }
            else bestl = 100000000000000;
        }
    }
    if(c-bestl <= bestr-c) cout << bestl << endl;
    else cout << bestr << endl;
    return 0;
}