#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int pos, n;
  cin >> n >> pos;
  int x, y;
  cin >> x >> y;
  int a = min(x, y);
  int b = max(x, y);
  for (int i = 0; i < n - 1; ++i) {
    cin >> x >> y;
    a = max(a, min(x, y));
    b = min(b, max(x, y));
  }
  if (b < a)
    cout << -1 << '\n';
  else {
    if (a <= pos and b >= pos)
      cout << 0 << '\n';
    else
      cout << min(fabs(a - pos), fabs(b - pos)) << '\n';
  }
  return 0;
}
