#include <bits/stdc++.h>
using namespace std;
long long a[500010];
map<long long, long long> m;
vector<long long> v;
string s, q;
int main() {
  long long t = 1, n, i, j, k, l, r, mn = 0, mx = 0, x, y;
  while (t--) {
    scanf("%lld%lld", &n, &k);
    l = 0, r = INT_MAX;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      scanf("%lld%lld", &x, &y);
      if (x > y) swap(x, y);
      l = max(l, x);
      r = min(r, y);
    }
    if (l > r) {
      printf("-1\n");
      return 0;
    } else if (k >= l && k <= r)
      ans = 0LL;
    else
      ans = min(abs(k - l), abs(k - r));
    printf("%lld", ans);
  }
}
