#include <bits/stdc++.h>
using namespace std;
int A[1000010];
int main() {
  int n, x0;
  cin >> n >> x0;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b) swap(a, b);
    for (int j = a; j <= b; j++) {
      A[j]++;
    }
  }
  int res = INT_MAX;
  for (int i = 0; i < 1000010; i++) {
    if (A[i] == n) {
      res = min(res, abs(x0 - i));
    }
  }
  if (res != INT_MAX)
    cout << res;
  else
    cout << -1;
  return 0;
}
