#include <bits/stdc++.h>
using namespace std;
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
int main() {
  long long i, j, k, n, p, t, a, b, kk;
  cin >> n >> p;
  a = 0;
  vector<pair<long long, long long> > v;
  for (long long(i) = (0); (i) < (n); ++(i)) {
    cin >> j >> k;
    if (j > k) {
      t = k;
      k = j;
      j = t;
    }
    if (j <= p && p <= k) {
      v.push_back({j, k});
      continue;
    } else {
      if (p < j) {
        a += j - p;
        p = j;
      } else {
        a += p - k;
        p = k;
      }
      t = 0;
      for (t = t; t < v.size(); t++) {
        if (!(v[t].first <= p && p <= v[t].second)) break;
      }
      if (t != v.size()) break;
      v.push_back({j, k});
    }
  }
  if (v.size() != n)
    cout << "-1";
  else
    cout << a;
}
