#include <bits/stdc++.h>
using namespace std;
int n, x;
int main() {
  cin >> n >> x;
  int lbound = 0, rbound = 1020;
  while (n--) {
    int l, r;
    cin >> l >> r;
    if (l > r) swap(l, r);
    lbound = max(lbound, l);
    rbound = min(rbound, r);
  }
  int l = lbound, r = rbound;
  if (l > r)
    cout << -1;
  else if (l <= x && x <= r)
    cout << 0;
  else if (x < l)
    cout << l - x;
  else if (x > r)
    cout << x - r;
  return 0;
}
