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

const ll INF = 1e18;

vector<pair<ll, ll>> Segments;

ll Solve(ll n, ll m, ll s)
{
    sort(Segments.begin(), Segments.end());

    ll end = 0; 
    pair<ll, ll> sol = {INF, 0};
    for(auto [l, r] : Segments) {
        if(end + 1 != l) {
            sol = min(sol, {abs(s - (l-1)), l-1});
            sol = min(sol, {abs(s - (end+1)), end+1});
        }
        end = r;
    }
    if(end < n)
        sol = min(sol, {abs(s - (end+1)), end+1});
    return sol.second;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll n, m, s, l, r;
    cin >> n >> m >> s;

    for(int i = 0; i < m; i++) {
        cin >> l >> r;
        Segments.push_back({l, r});
    }

    cout << Solve(n, m, s) << "\n";
    return 0;
}