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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <memory.h>
#include <stdio.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <ranges>
#include <locale>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <string_view>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

#define fst first
#define snd second
#define X first
#define Y second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

// #define next ajksdslk
// #define prev aklsfjk

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template <typename T1, typename T2>
inline bool upmax(T1& a, T2 b) {
  return (a < b ? (a = b, true) : false);
}

template <typename T1, typename T2>
inline bool upmin(T1& a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

const int maxn = (int)(1e6 + 10);
const int maxlog = 21;
const int base = (int)1e9 + 7;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
// const int pp = 41;
const int inf = 1e9;
const ll llinf = 2e18;

const int MOD = 998244353;

void solve() {
  ll n, m, s; cin >> n >> m >> s;
  vector<pll> a(m);
  for (auto&[l, r] : a) {
    cin >> l >> r;
  }
  sort(all(a));
  vector<pll> b;
  for (auto&[l, r] : a) {
    if (b.empty() || b.back().snd + 1 < l) {
      b.emplace_back(l, r);
    } else {
      b.back().snd = r;
    }
  }
  // for(auto&[l, r] : b) {
  //   cout << l << " " << r << endl;
  // }
  
  ll curDist = llinf, point = -1;
  for (auto&[l, r] : b) {
    if (l <= s && s <= r) {
      if ( 1 <= l - 1 && l - 1 <= n) {
        if (upmin(curDist, abs(s - (l - 1)))) {
          point = l - 1;
        }
      }
      if ( 1 <= r + 1 && r + 1 <= n) {
        if (upmin(curDist, abs(s - (r + 1)))) {
          point = r + 1;
        }
      }
      break;
    }
  }
  assert(point != -1);
  cout << point << endl;
}

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

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  
  
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    // cout << "Case #" << i << ": ";
    solve();
  }
  
  return 0;
  
  
  
}