#include <bits/stdc++.h>
using namespace std;
int l[5005];
int ls[5005];
int r[5005];
int rs[5005];
int sx, sxs;
int x[10005];
long long a[2][10005];
int getDist(int pos, int id) {
  if (pos >= l[id] && pos <= r[id]) return 0;
  return min(abs(pos - l[id]), abs(pos - r[id]));
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%d", &sx);
  int m = 2 * n + 1;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &l[i], &r[i]);
    x[i * 2] = l[i];
    x[i * 2 + 1] = r[i];
  }
  x[m - 1] = sx;
  sort(x, x + m);
  m = unique(x, x + m) - x;
  memset(a, -1, sizeof(a));
  int cur = 0;
  for (int i = 0; i < m; i++) a[0][i] = abs(x[i] - sx);
  for (int t = 0; t < n; t++, cur = !cur) {
    a[!cur][0] = a[cur][0];
    for (int i = 1; i < m; i++) {
      a[!cur][i] = min(a[!cur][i - 1] + x[i] - x[i - 1], a[cur][i]);
    }
    for (int i = m - 2; i >= 0; i--) {
      a[!cur][i] = min(a[!cur][i + 1] + x[i + 1] - x[i], a[!cur][i]);
    }
    for (int i = 0; i < m; i++) {
      a[!cur][i] += getDist(x[i], t);
    }
  }
  long long ans = -1;
  for (int i = 0; i < m; i++) {
    if (a[cur][i] != -1 && (ans < 0 || ans > a[cur][i])) ans = a[cur][i];
  }
  cout << ans << endl;
  return 0;
}
