#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int x;
  int v[1001][2];
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1];
    if (v[i][0] > v[i][1]) swap(v[i][0], v[i][1]);
  }
  int best = 1000000000;
  for (int i = 0; i <= 1000; i++) {
    if (abs(i - x) >= best) continue;
    bool good = true;
    for (int j = 0; j < n; j++) {
      if (i >= v[j][0] && i <= v[j][1])
        continue;
      else {
        good = false;
        break;
      }
    }
    if (good) best = abs(i - x);
  }
  if (best == 1000000000)
    cout << -1 << endl;
  else
    cout << best;
  return 0;
}
