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
68
69
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <numeric>
#include <stack>
using namespace std;
using ll = long long int;
#define debug(x) cout << #x << " = " << x << endl;
void solve()
{
    ll n, s;
    ll m;
    cin >> n >> m >> s;
    vector<pair<ll, ll>>v(m);
    for (int i = 0; i < m; i++)
    {
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    vector<pair<ll, ll>>wolne;
    ll pocz = 1;
    for (int i = 0; i < m; i++)
    {
        if (v[i].first - 1 >= pocz)
        {
            wolne.push_back({ pocz,v[i].first - 1 });
        }
        pocz = v[i].second + 1;
    }
    if (n >= pocz)
        wolne.push_back({ pocz,n });

    ll odl = 1e18;
    ll odp = -1;
    for (int i = 0; i < wolne.size(); i++)
    {
        ll dl = abs(s - wolne[i].first);
        if (odl > dl)
        {
            odl = dl;
            odp = wolne[i].first;
        }

        dl = abs(s - wolne[i].second);
        if (odl > dl)
        {
            odl = dl;
            odp = wolne[i].second;
        }
    }
    cout << odp << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
    //int t;
    //cin >> t;
    //while (t--)
    //{
    solve();
    //}
    return 0;
}