#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:134217728")
using namespace std;
const long long INF = 1e18;
const int MAX_N = 5005;
pair<int, int> pts[MAX_N];
int idpt[2 * MAX_N];
map<int, int> ptid;
long long dp[2][2 * MAX_N];
set<int> s;
int getDst(int x, int i) {
  if (x >= pts[i].first && x <= pts[i].second) {
    return 0;
  }
  return min(abs(x - pts[i].first), abs(x - pts[i].second));
}
void relax(long long &var, long long x) { var = min(var, x); }
int main() {
  int n, x;
  cin >> n >> x;
  s.insert(x);
  for (int i = 0; i < n; ++i) {
    cin >> pts[i].first >> pts[i].second;
    s.insert(pts[i].first);
    s.insert(pts[i].second);
  }
  int id = 0;
  for (auto y : s) {
    idpt[id] = y;
    ptid[y] = id;
    ++id;
  }
  for (int j = 0; j < id; ++j) {
    dp[0][j] = INF;
  }
  dp[0][ptid[x]] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < id; ++j) {
      dp[1][j] = INF;
    }
    long long bestDst = dp[0][0];
    relax(dp[1][0], dp[0][0] + getDst(idpt[0], i));
    for (int j = 1; j < id; ++j) {
      int dst = idpt[j] - idpt[j - 1];
      bestDst += dst;
      relax(bestDst, dp[0][j]);
      relax(dp[1][j], bestDst + getDst(idpt[j], i));
    }
    bestDst = dp[0][id - 1];
    relax(dp[1][id - 1], dp[0][id - 1] + getDst(idpt[id - 1], i));
    for (int j = id - 2; j >= 0; --j) {
      int dst = idpt[j + 1] - idpt[j];
      bestDst += dst;
      relax(bestDst, dp[0][j]);
      relax(dp[1][j], bestDst + getDst(idpt[j], i));
    }
    for (int j = 0; j < id; ++j) {
      dp[0][j] = dp[1][j];
    }
  }
  long long best = INF;
  for (int i = 0; i < id; ++i) {
    relax(best, dp[0][i]);
  }
  cout << best << endl;
  return 0;
}
