#include <bits/stdc++.h>
using namespace std;
const int maxn = 1006;
int a[maxn];
int main() {
  int n, x;
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    if (l > r) swap(l, r);
    a[l]++;
    a[r + 1]--;
  }
  int tmp = 0;
  int ans = 1e9;
  for (int i = 0; i <= 1000; i++) {
    tmp += a[i];
    if (tmp == n) ans = min(ans, abs(i - x));
  }
  if (ans == 1e9)
    cout << "-1" << endl;
  else
    cout << ans << endl;
}
