#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void Read(T &x) {
  int f = 1;
  char t = getchar();
  while (t < '0' || t > '9') {
    if (t == '-') f = -1;
    t = getchar();
  }
  x = 0;
  while (t >= '0' && t <= '9') {
    x = x * 10 + t - '0';
    t = getchar();
  }
  x *= f;
}
const int maxn = 5005;
const int inf = 0x3f3f3f3f;
int n, x, pos[maxn << 1], ln;
long long f[maxn << 1];
int l[maxn << 1], r[maxn << 1];
pair<int, int> g[maxn];
void input() {
  Read(n), Read(x);
  for (register int i = 1; i <= n; i++) {
    Read(g[i].first), Read(g[i].second);
    pos[++ln] = g[i].first, pos[++ln] = g[i].second;
  }
  pos[++ln] = x;
  sort(pos + 1, pos + ln + 1);
  ln = unique(pos + 1, pos + ln + 1) - pos - 1;
}
void solve() {
  for (register int i = 1; i <= n; i++) {
    l[i] = lower_bound(pos + 1, pos + ln + 1, g[i].first) - pos;
    r[i] = lower_bound(pos + 1, pos + ln + 1, g[i].second) - pos;
  }
  int st = lower_bound(pos + 1, pos + ln + 1, x) - pos;
  long long ans;
  memset(f, inf, sizeof(f)), f[st] = 0;
  for (register int i = 1; i <= n; i++) {
    long long minn = f[1];
    for (register int j = 2; j <= ln; j++) {
      minn = min(minn + pos[j] - pos[j - 1], f[j]);
      f[j] = minn;
    }
    minn = f[ln];
    for (register int j = ln - 1; j >= 1; j--) {
      minn = min(minn + pos[j + 1] - pos[j], f[j]);
      f[j] = minn;
    }
    for (register int j = 1; j <= ln; j++) {
      if (j >= l[i] && j <= r[i]) continue;
      f[j] += min(abs(pos[j] - pos[l[i]]), abs(pos[j] - pos[r[i]]));
    }
  }
  ans = f[1];
  for (register int i = 2; i <= ln; i++) ans = min(ans, f[i]);
  cout << ans << endl;
}
int main() {
  input();
  solve();
  return 0;
}
