#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    ;
  do {
    s = s * 10 + c - '0';
  } while ((c = getchar()) >= '0' && c <= '9');
  return s;
}
const int N = 5010;
typedef long long arr[N * 2];
int n, x, l[N], r[N], mx, tot, tt, yv[N * 2], ps[N * 2];
long long ans, sl[N * 2], sr[N * 2];
arr f[2], *pt;
int main() {
  int i, j, p, ad;
  n = read();
  x = read();
  ps[tot = 1] = x;
  for (i = 1; i <= n; i++)
    ps[++tot] = l[i] = read(), ps[++tot] = r[i] = read(),
    sort(ps + 1, ps + 1 + tot);
  for (i = 1; i <= tot; i++)
    if (ps[i] != ps[i - 1]) yv[++tt] = ps[i];
  for (i = 1; i <= tt; i++) f[0][i] = abs(yv[i] - x);
  memset(sl, 0x7f, sizeof sl), memset(sr, 0x7f, sizeof sr);
  for (p = 0, i = 1; i <= n; i++) {
    p ^= 1;
    pt = &f[p];
    for (j = 1; j <= tt; j++) {
      x = yv[j];
      f[p][j] = f[p ^ 1][j];
      if (sl[j - 1] + x < (*pt)[j]) (*pt)[j] = sl[j - 1] + x;
      if (sr[j + 1] - x < (*pt)[j]) (*pt)[j] = sr[j + 1] - x;
    }
    for (j = 1; j <= tt; j++) {
      if (yv[j] >= l[i]) break;
      (*pt)[j] += l[i] - yv[j];
    }
    for (j = tt; j >= 1; j--) {
      if (yv[j] <= r[i]) break;
      (*pt)[j] += yv[j] - r[i];
    }
    for (j = 1; j <= tt; j++)
      sl[j] =
          ((sl[j - 1]) < ((*pt)[j] - yv[j]) ? (sl[j - 1]) : ((*pt)[j] - yv[j]));
    for (j = tt; j >= 1; j--)
      sr[j] =
          ((sr[j + 1]) < ((*pt)[j] + yv[j]) ? (sr[j + 1]) : ((*pt)[j] + yv[j]));
  }
  ans = f[p][1];
  for (i = 2; i <= tt; i++)
    if (f[p][i] < ans) ans = f[p][i];
  printf("%I64d\n", ans);
  return 0;
}
