#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int ID[maxn << 1];
int num = 0;
struct Discrete {
  vector<int> val;
  int cnt;
  void init() {
    cnt = 0;
    val.clear();
  }
  void add(int x) { val.push_back(x); }
  void solve() {
    sort(val.begin(), val.end());
    val.resize(unique(val.begin(), val.end()) - val.begin());
    num = val.size();
    for (int i = 0; i < num; i++) ID[i] = val[i];
  }
  int find(int x) {
    int ret = lower_bound(val.begin(), val.end(), x) - val.begin();
    return ret;
  }
} D;
struct node {
  int l, r;
} P[maxn];
int n, S, idS;
void init() {
  scanf("%d%d", &n, &S);
  for (int i = (1); i <= (n); i++) scanf("%d%d", &P[i].l, &P[i].r);
  D.init();
  D.add(S);
  for (int i = (1); i <= (n); i++) D.add(P[i].l), D.add(P[i].r);
  D.solve();
  idS = D.find(S);
}
long long f[2][maxn];
void solve() {
  for (int i = 0; i < num; i++) f[0][i] = abs(ID[i] - S);
  int cur = 0;
  for (int i = 1; i <= n; i++) {
    cur ^= 1;
    memset(f[cur], 0, sizeof(cur));
    int bestP = 0;
    for (int j = 0; j < num; j++) {
      if (f[cur ^ 1][bestP] + abs(ID[bestP] - ID[j]) > f[cur ^ 1][j]) bestP = j;
      f[cur][j] = f[cur ^ 1][bestP] + abs(ID[bestP] - ID[j]);
    }
    bestP = num - 1;
    for (int j = num - 1; j >= 0; j--) {
      if (f[cur ^ 1][bestP] + abs(ID[bestP] - ID[j]) > f[cur ^ 1][j]) bestP = j;
      f[cur][j] = min(f[cur][j], f[cur ^ 1][bestP] + abs(ID[bestP] - ID[j]));
    }
    for (int j = 0; j < num; j++) {
      if (ID[j] >= P[i].l && ID[j] <= P[i].r) continue;
      f[cur][j] += min(abs(P[i].l - ID[j]), abs(P[i].r - ID[j]));
    }
  }
  long long ans = f[cur][0];
  for (int i = 1; i < num; i++) ans = min(ans, f[cur][i]);
  cout << ans << endl;
}
int main() {
  init();
  solve();
  return 0;
}
