#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
bool cmpManh(const std::pair<long long, long long>& l,
             const std::pair<long long, long long>& r) {
  return ((llabs(l.first) + llabs(l.second)) <
          (llabs(r.first) + llabs(r.second)));
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int main(void) {
  int n;
  scanf("%d", &n);
  int x;
  scanf("%d", &x);
  int ans = 1e9;
  map<int, int> mp;
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    int b;
    scanf("%d", &b);
    if (a > b) {
      swap(a, b);
    }
    mp[a]++;
    mp[b + 1]--;
  }
  for (int i = 0; i <= 1002; ++i) {
    mp[i] = mp[i - 1] + mp[i];
  }
  for (int i = 0; i < 10002; ++i) {
    int dist;
    if (mp[i] >= n) {
      dist = abs(x - i);
      ans = min(ans, dist);
    }
  }
  if (ans == 1e9) {
    cout << -1 << '\n';
  } else {
    cout << ans << '\n';
  }
  return (0);
}
