#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1;
int n, m;
int x, y;
int minn = INT_MAX, maxn = INT_MIN;
int dis;
inline int read() {
  int X = 0;
  bool flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    X = (X << 1) + (X << 3) + ch - '0';
    ch = getchar();
  }
  if (flag) return X;
  return ~(X - 1);
}
inline void write(int X) {
  if (X < 0) {
    X = ~(X - 1);
    putchar('-');
  }
  if (X > 9) write(X / 10);
  putchar(X % 10 + '0');
}
int main() {
  n = read(), m = read();
  for (int i = 0; i < n; i++) {
    x = read(), y = read();
    if (x > y) swap(x, y);
    if (x > maxn) maxn = x;
    if (y < minn) minn = y;
  }
  if (maxn > minn)
    write(-1);
  else if (m >= maxn && m <= minn)
    write(0);
  else {
    dis = min(abs(m - minn), abs(m - maxn));
    write(dis);
  }
  return 0;
}
