#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
pair<int, int> seg[maxn];
bool isInSeg(int x, pair<int, int> p) {
  return x <= p.second && x >= p.first ? true : false;
}
int main() {
  int n, s, l, r;
  bool flag = true;
  scanf("%d %d", &n, &s);
  int lef = -1, rig = 1005;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &seg[i].first, &seg[i].second);
    if (seg[i].first > seg[i].second) swap(seg[i].first, seg[i].second);
    for (int j = i - 1; flag && j >= 0; j--) {
      if (!(isInSeg(seg[i].first, seg[j]) || isInSeg(seg[i].second, seg[j]) ||
            isInSeg(seg[j].first, seg[i]) || isInSeg(seg[j].second, seg[i])))
        flag = false;
    }
    lef = max(lef, seg[i].first);
    rig = min(rig, seg[i].second);
  }
  if (!flag)
    printf("-1\n");
  else if (isInSeg(s, pair<int, int>(lef, rig)))
    printf("0\n");
  else if (s < lef)
    printf("%d\n", lef - s);
  else
    printf("%d\n", s - rig);
  return 0;
}
