#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, cnt = 0, sum = 0, a, b, i;
  cin >> n >> x;
  vector<pair<int, int> > v;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    if (a > b) swap(a, b);
    v.push_back(make_pair(a, b));
  }
  sort(v.begin(), v.end());
  a = v[0].first;
  b = v[0].second;
  for (i = 1; i < n; i++) {
    if (b < v[i].first || a > v[i].second) {
      printf("-1\n");
      return 0;
    } else {
      a = max(a, v[i].first);
      b = min(b, v[i].second);
    }
  }
  if (x < a || x > b) sum += min(abs(x - a), abs(x - b));
  printf("%d\n", sum);
  return 0;
}
