#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int a[1005];
int main() {
  int l, r, n, x;
  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> l >> r;
    if (l > r) swap(l, r);
    a[l]++;
    a[r + 1]--;
  }
  int tmp1 = 0;
  int minn = 1e4 + 4;
  for (int i = 0; i <= 1000; i++) {
    tmp1 += a[i];
    if (tmp1 == n) minn = min(minn, abs(i - x));
  }
  if (minn == 1e4 + 4)
    cout << "-1\n";
  else
    cout << minn << endl;
  return 0;
}
