#include <bits/stdc++.h>
int main() {
  int a[1000], b[10000], max1, i, j, k, l, m, n, c, cnt, min1, d, r, s;
  while (scanf("%d %d", &m, &n) == 2) {
    max1 = 0;
    min1 = s = r = 88899;
    for (i = 1; i <= m; i++) {
      scanf("%d%d", &a[i], &b[i]);
      l = (a[i] < b[i] ? a[i] : b[i]);
      k = (a[i] > b[i] ? a[i] : b[i]);
      a[i] = l;
      b[i] = k;
      if (max1 < k) max1 = k;
      if (min1 > l) min1 = l;
    }
    int ans;
    d = 0;
    for (i = n; i <= max1; i++) {
      cnt = 0;
      for (j = 1; j <= m; j++) {
        if (i >= a[j] && i <= b[j])
          ++cnt;
        else
          break;
      }
      if (cnt == m) {
        r = i - n;
        d = 1;
        break;
      }
    }
    for (i = n; i >= min1; i--) {
      cnt = 0;
      for (j = 1; j <= m; j++) {
        if (i >= a[j] && i <= b[j])
          ++cnt;
        else
          break;
      }
      if (cnt == m) {
        s = n - i;
        d = 1;
        break;
      }
    }
    if (d == 0)
      printf("-1\n");
    else
      printf("%d\n", (r < s ? r : s));
  }
  return 0;
}
