#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n >> x;
  int l1, l2;
  cin >> l1 >> l2;
  if (l1 > l2) swap(l1, l2);
  n--;
  while (n--) {
    int x, y;
    cin >> x >> y;
    if (x > y) swap(x, y);
    if ((x <= l1 && y >= l1) || (x <= l2 && y >= l2) || (x >= l1 && y <= l2)) {
      l1 = max(l1, x);
      l2 = min(l2, y);
    } else {
      cout << "-1";
      return 0;
    }
  }
  if (x >= l1 && x <= l2) {
    cout << "0";
  } else {
    cout << min(abs(x - l1), abs(l2 - x));
  }
}
