#include <bits/stdc++.h>
int main() {
  int x, n, i, p, q, j, ans1 = 1002, ans2 = 1002, z, flag = 0;
  scanf("%d %d", &n, &x);
  int arr[n][2], arr2[1001];
  for (i = 0; i < 1001; i++) arr2[i] = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j <= 1; j++) {
      scanf("%d", &arr[i][j]);
    }
  }
  for (i = 0; i < n; i++) {
    p = arr[i][0];
    q = arr[i][1];
    if (p >= q) {
      for (z = q; z <= p; z++) arr2[z]++;
    } else {
      for (z = p; z <= q; z++) arr2[z]++;
    }
  }
  for (i = x;; i--) {
    if (arr2[i] == n) {
      ans1 = x - i;
      flag = 0;
      break;
    } else if (i == 0) {
      flag++;
      break;
    }
  }
  for (i = x;; i++) {
    if (arr2[i] == n) {
      ans2 = i - x;
      break;
    } else if (i == 1000) {
      flag++;
      break;
    }
  }
  if (flag != 2) {
    if (ans1 <= ans2)
      printf("%d", ans1);
    else
      printf("%d", ans2);
  } else
    printf("-1");
  return 0;
}
