#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 1e3 + 2;
const long long int mod = 1e3 + 2;
vector<long long int> d;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, x, ans = INT_MAX;
  cin >> n >> x;
  long long int minx = x, maxx = x, a[maxn];
  memset(a, 0, sizeof(a));
  for (long long int i = 0; i < n; i++) {
    long long int start, stop;
    cin >> start >> stop;
    if (stop < start) swap(start, stop);
    for (long long int j = start; j <= stop; j++) ++a[j];
  }
  for (long long int i = 0; i <= 1003; i++)
    if (a[i] == n) ans = min(abs(x - i), ans);
  if (ans == INT_MAX)
    cout << "-1" << endl;
  else
    cout << ans << endl;
  return 0;
}
