#include <bits/stdc++.h>
using namespace std;
int const OO = (1 << 28);
int main() {
  int mn = OO, mx = -1;
  int N, X;
  cin >> N >> X;
  vector<pair<int, int> > V(N);
  for (int i = 0; i < N; ++i) {
    cin >> V[i].first >> V[i].second;
    if (V[i].first > V[i].second) swap(V[i].first, V[i].second);
    mn = min(V[i].first, mn);
    mx = max(mx, V[i].second);
  }
  int m = OO;
  for (int i = mn; i <= mx; i++) {
    int c = 0;
    for (int j = 0; j < N; ++j) {
      c += (i >= V[j].first && i <= V[j].second);
    }
    if (c == N) m = min(m, abs(i - X));
  }
  if (m == OO) m = -1;
  cout << m << endl;
}
