#include <bits/stdc++.h>
using namespace std;
int min(int a, int b) { return (a < b ? a : b); }
int main() {
  int n, x0, l = 0, r = 1001;
  cin >> n >> x0;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) {
      int t = x;
      x = y;
      y = t;
    }
    if (x > l) l = x;
    if (y < r) r = y;
  }
  if (r < l)
    cout << -1;
  else {
    if (x0 >= l && x0 <= r)
      cout << 0;
    else
      cout << (min(abs(x0 - l), abs(x0 - r)));
  }
  return 0;
}
