#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, l, r, lmax = 0, rmin = 999999999;
  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> l >> r;
    if (l > r) swap(l, r);
    if (l > lmax) lmax = l;
    if (r < rmin) rmin = r;
  }
  if (lmax > rmin) {
    cout << -1;
    return 0;
  }
  if (x >= lmax && x <= rmin)
    cout << 0;
  else
    cout << min(abs(x - lmax), abs(x - rmin));
  return 0;
}
