#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  cin >> n >> x;
  int i, j, a, b;
  int arr[1005] = {0};
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    if (a > b) {
      int t = a;
      a = b;
      b = t;
    }
    for (j = a; j <= b; j++) {
      arr[j]++;
    }
  }
  int st = -1, end = -1;
  for (i = 0; i < 1005; i++) {
    if (arr[i] == n && st == -1 && end == -1) {
      st = i;
    } else if (st != -1 && arr[i] != n) {
      end = i - 1;
      break;
    }
  }
  if (st == -1 && end == -1)
    cout << -1 << endl;
  else if (x >= st && x <= end)
    cout << 0 << endl;
  else
    cout << min(abs(st - x), abs(x - end)) << endl;
  return 0;
}
