#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x0;
  cin >> n >> x0;
  const int Max = 1010;
  int tbl[Max];
  memset(tbl, 0, sizeof(tbl));
  for (int i = 0; i < n; ++i) {
    int start, end;
    cin >> start >> end;
    if (start > end) {
      swap(start, end);
    }
    for (int i = start; i <= end; ++i) {
      tbl[i]++;
    }
  }
  int ans = INT_MAX;
  for (int i = 0; i < Max; ++i) {
    if (tbl[i] == n) {
      ans = min(ans, abs(i - x0));
    }
  }
  if (ans == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
