#include <bits/stdc++.h>
using namespace std;
const long long N = 1e3 + 10;
long long n, x, a, b, mx, mxx, mn = INT_MAX;
long long arr[N];
vector<long long> v;
void solve() {
  cin >> n >> x;
  for (long long i = 1; i <= n; i++) {
    cin >> a >> b;
    if (a > b) swap(a, b);
    arr[a] += 1;
    arr[b + 1] += -1;
    mx = max(mx, b);
  }
  for (long long i = 1; i <= mx; i++) {
    arr[i] += arr[i - 1];
    if (arr[i] > mxx)
      mxx = arr[i], v.clear(), v.push_back(i);
    else if (arr[i] == mxx)
      v.push_back(i);
  }
  if (mxx != n) {
    cout << -1 << endl;
    exit(0);
  }
  for (auto i : v) {
    mn = min(mn, abs(x - i));
  }
  cout << mn << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
