#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  scanf("%d%d", &n, &x);
  int mn = 0, mx = 1e9;
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a > b) swap(a, b);
    mn = max(mn, a);
    mx = min(mx, b);
  }
  if (mn > mx)
    printf("-1");
  else if (x >= mn && x <= mx)
    printf("0");
  else
    cout << min(abs(x - mn), abs(x - mx));
  return 0;
}
