#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[1001];
  for (int i = 0; i < 1001; i++) a[i] = 0;
  int N, x0;
  cin >> N >> x0;
  for (int i = 0; i < N; i++) {
    int b, e;
    cin >> b >> e;
    if (b > e) swap(b, e);
    for (int j = b; j <= e; j++) a[j]++;
  }
  int ret = 1001;
  for (int i = 0; i <= 1000; i++) {
    if (a[i] == N) {
      ret = min(ret, abs(x0 - i));
    }
  }
  if (ret == 1001)
    cout << -1 << endl;
  else
    cout << ret << endl;
  return 0;
}
