#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, a, b, l, r;
  cin >> n >> x;
  cin >> a >> b;
  if (a > b) swap(a, b);
  l = a, r = b;
  for (int i = 2; i <= n; ++i) {
    cin >> a >> b;
    if (a > b) swap(a, b);
    if (a <= r && a >= l) {
      if (b >= r)
        l = a;
      else
        l = a, r = b;
    } else if (b <= r && b >= l) {
      if (a >= l)
        l = a, r = b;
      else
        r = b;
    } else if (a <= r && b >= l) {
    } else {
      printf("-1\n");
      return 0;
    }
  }
  if (x >= l && x <= r) {
    printf("0\n");
  } else {
    printf("%d\n", min(abs(r - x), abs(l - x)));
  }
  return 0;
}
