#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, mn = 1005;
  cin >> n >> k;
  int a[n], b[n];
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
  for (int i = 0; i < 1001; i++) {
    bool f = 0;
    for (int j = 0; j < n; j++) {
      if (i < min(a[j], b[j]) || i > max(a[j], b[j])) f = 1;
    }
    if (f == 0 && abs(i - k) < mn) mn = abs(i - k);
  }
  if (mn == 1005)
    cout << -1 << endl;
  else
    cout << mn << endl;
  return 0;
}
