#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
using namespace std;
void fastio() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
long long dp[101][1001], ans1 = 1e9, dp1[1000001];
long long num[10];
long long a[1000000];
signed main() {
  fastio();
  long long t = 1, x1 = 1;
  while (t--) {
    long long n, x;
    cin >> n >> x;
    long long a, b, l = 0, r = 1000000;
    while (n--) {
      cin >> a >> b;
      if (a > b) swap(a, b);
      l = max(l, a);
      r = min(r, b);
    }
    if (l > r)
      cout << "-1";
    else if (x >= l && x <= r)
      cout << 0;
    else
      cout << min(abs(x - l), abs(x - r));
  }
}
