#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long x, y, mi, ma;
  cin >> x >> y;
  if (x < y) {
    mi = x;
    ma = y;
  } else {
    mi = y;
    ma = x;
  }
  for (long long i = 1; i < n; i++) {
    cin >> x >> y;
    if (x > y) {
      long long z = x;
      x = y;
      y = z;
    }
    if (x > mi) mi = x;
    if (y < ma) ma = y;
  }
  if (mi <= ma) {
    if (mi <= m && m <= ma)
      cout << 0;
    else if (m > ma)
      cout << m - ma;
    else
      cout << mi - m;
  } else
    cout << -1;
}
