#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline string gets() {
  char ch[600];
  scanf("%s", ch);
  return ch;
}
template <class P, class Q>
inline P smin(P &a, Q b) {
  if (b < a) a = b;
  return a;
}
template <class P, class Q>
inline P smax(P &a, Q b) {
  if (a < b) a = b;
  return a;
}
const int MAX_LG = 17;
const long long maxn = 2000 + 10;
const long long base = 29;
const long long mod = 1e9 + 9;
const long long INF = 1e9;
const long long maxq = 2e5 + 10;
const long long SQ = sqrt(maxn);
int32_t main() {
  long long n = in(), x0 = in();
  long long mini = 1e9, maxi = -1e9;
  for (long long i = 0; i < n; i++) {
    long long a = in(), b = in();
    if (a > b) swap(a, b);
    mini = min(mini, b);
    maxi = max(maxi, a);
  }
  if (maxi > mini) {
    cout << -1 << "\n";
  } else {
    long long res = 1e9;
    for (long long i = maxi; i <= mini; i++) res = min(res, abs(i - x0));
    cout << res << "\n";
  }
}
