#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  pair<int, int> arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i].first >> arr[i].second;
    int mini = min(arr[i].first, arr[i].second);
    arr[i].second = arr[i].first + arr[i].second - mini;
    arr[i].first = mini;
  }
  sort(arr, arr + n);
  int low = arr[0].first, high = arr[0].second;
  for (int i = 0; i < n - 1; ++i) {
    int currl = arr[i + 1].first;
    int currh = arr[i + 1].second;
    if (currl >= low && currl <= high) {
      low = currl;
    } else {
      cout << -1;
      return 0;
    }
    if (currh < high) {
      high = currh;
    }
  }
  if (m >= low && m <= high) {
    cout << 0;
  } else if (m < low) {
    cout << low - m;
  } else {
    cout << m - high;
  }
  return 0;
}
