#include <bits/stdc++.h>
int min = 1000000000, a, b, n, x, A[1111];
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &b);
    if (a > b) std::swap(a, b);
    for (int j = a; j <= b; j++) ++A[j];
  }
  for (int i = 0; i < 1001; i++)
    if (A[i] == n && abs(x - i) < min) min = abs(x - i);
  if (min == 1000000000)
    puts("-1");
  else
    printf("%d\n", min);
  return 0;
}
