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
#include <cstdio>
#include <limits>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

long long s, n; 
long long best = numeric_limits<long long>::max();
long long res = -1;

inline void check(long long a, long long b) {
  if (a <= b) {
    if (abs(s-a) < best) {
      best = abs(s-a);
      res = a;
    }
    if (abs(s-b) < best) {
      best = abs(s-b);
      res = b;
    }
  }
}

int main() {
  int m; scanf(" %lld %d %lld", &n, &m, &s);

  vector<pair<long long, long long> > t(m + 2);
  for (int i=0; i<m; ++i) {
    long long a, b; scanf(" %lld %lld", &a, &b);
    t[i] = make_pair(a-1, b+1);
  }
  t[m] = make_pair(-1, 1);
  t[m+1] = make_pair(n, n+2);
  sort(t.begin(), t.end());

  for (int i=0; i<m+1; ++i) {
    check(t[i].second, t[i+1].first);
  }

  printf("%lld\n", res);

  return 0;
}