#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool stb[70] = {0};
bool st[70] = {0};
void solve() {
  ll l = 0, r = 10000;
  ll n, x;
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    if (a > b) swap(a, b);
    if (a > l) l = a;
    if (b < r) r = b;
  }
  if (l > r)
    cout << -1;
  else if (x >= l && x <= r)
    cout << 0;
  else
    cout << min(abs(x - r), abs(x - l));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
