#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, a, b, x, l = -1, r = 1e9;
  cin >> n >> x;
  for (int qq = 0; qq < n; qq++) {
    cin >> a >> b;
    if (a > b) swap(a, b);
    l = max(l, a);
    r = min(r, b);
  }
  if (l > r)
    puts("-1\n");
  else if (x >= r)
    cout << x - r << endl;
  else if (x >= l && x <= r)
    puts("0\n");
  else
    cout << (l - x) << endl;
}
