#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000")
using namespace std;
inline long long read() {
  long long x;
  cin >> x;
  return x;
}
const int MAX_N = 3e5 + 5;
const long long oo = 1e18;
int n, nPoint, start;
int l[MAX_N], r[MAX_N], a[MAX_N];
long long f[2][MAX_N], gleft[2][MAX_N], gright[2][MAX_N];
map<int, int> q;
inline void push(int x, int y) {
  if (x > y) swap(x, y);
  q[x], q[y];
  int mid = (x + y) >> 1;
  for (int i = (-1); i <= (1); ++i) {
    int t = mid + i;
    t = min(t, y);
    t = max(t, x);
    q[t];
  }
}
inline long long getDist(int pos, int i) {
  if (pos < l[i]) return l[i] - pos;
  if (pos > r[i]) return pos - r[i];
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(9);
  cout.setf(ios::fixed, ios::floatfield);
  cin >> n >> start;
  q.clear();
  q[start];
  for (int i = (1); i <= (n); ++i) {
    cin >> l[i] >> r[i];
    push(start, l[i]);
    push(start, r[i]);
    push(l[i], r[i]);
  }
  int nPoint = 0;
  for (__typeof(q.begin()) it = q.begin(); it != q.end(); it++)
    a[++nPoint] = it->first;
  int t = 0;
  for (int i = (0); i <= (nPoint); ++i)
    for (int x = (0); x <= (1); ++x) {
      f[x][i] = oo;
      gleft[x][i] = oo;
      gright[x][i] = oo;
    }
  for (int i = (1); i <= (nPoint); ++i)
    if (a[i] == start) {
      f[0][i] = 0;
      for (int j = (i); j <= (nPoint); ++j) gleft[0][j] = f[0][i] - a[i];
      for (int j = (i); j >= (1); --j) gright[0][j] = f[0][i] + a[i];
      break;
    }
  for (int turn = (1); turn <= (n); ++turn) {
    int tt = t;
    t ^= 1;
    for (int i = (1); i <= (nPoint); ++i) {
      long long tmp = getDist(a[i], turn);
      f[t][i] = oo;
      f[t][i] = min(f[t][i], gleft[tt][i] + a[i] + tmp);
      f[t][i] = min(f[t][i], gright[tt][i] - a[i] + tmp);
    }
    gleft[t][0] = gright[t][nPoint + 1] = oo;
    for (int i = (1); i <= (nPoint); ++i)
      gleft[t][i] = min(gleft[t][i - 1], f[t][i] - a[i]);
    for (int i = (nPoint); i >= (1); --i)
      gright[t][i] = min(gright[t][i + 1], f[t][i] + a[i]);
  }
  long long ans = oo;
  for (int i = (1); i <= (nPoint); ++i) ans = min(ans, f[t][i]);
  cout << ans << '\n';
  return 0;
}
