#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 7;
const int MAXN = (int)1e5 + 7;
int n, x;
int l[MAXN];
int r[MAXN];
long long dp[2][MAXN];
vector<int> pt;
inline void compress() {
  pt.push_back(x);
  for (int i = 1; i <= n; i++) {
    pt.push_back(l[i]);
    pt.push_back(r[i]);
  }
  sort(pt.begin(), pt.end());
  pt.resize(unique(pt.begin(), pt.end()) - pt.begin());
}
int getId(int coord) {
  return lower_bound(pt.begin(), pt.end(), coord) - pt.begin();
}
int dist(int idx, int idy) { return abs(pt[idx] - pt[idy]); }
int upto[MAXN];
long long pref[MAXN];
long long suff[MAXN];
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &l[i], &r[i]);
  }
  compress();
  int sz = pt.size();
  for (int cur = 0; cur < pt.size(); cur++) {
    dp[0][cur] = (long long)1e18;
    dp[1][cur] = (long long)1e18;
  }
  for (int i = 0; i < pt.size(); i++) {
    int cx = pt[i];
    upto[i] = lower_bound(pt.begin(), pt.end(), cx) - pt.begin();
  }
  dp[0][getId(x)] = 0;
  for (int i = 1; i <= n; i++) {
    pref[0] = dp[0][0] - pt[0];
    for (int j = 1; j < sz; j++) pref[j] = min(pref[j - 1], dp[0][j] - pt[j]);
    suff[sz - 1] = dp[0][sz - 1] + pt[sz - 1];
    for (int j = sz - 2; j >= 0; j--)
      suff[j] = min(suff[j + 1], dp[0][j] + pt[j]);
    for (int cur = 0; cur < sz; cur++) {
      int cx = pt[cur];
      long long penalty = 0;
      if (l[i] <= cx && cx <= r[i])
        penalty = 0;
      else
        penalty = min(abs(cx - l[i]), abs(cx - r[i]));
      int go = min(sz, upto[cur]);
      dp[1][cur] = min(dp[1][cur], pref[go] + pt[cur]);
      if (go + 1 < sz) dp[1][cur] = min(dp[1][cur], suff[go + 1] - pt[cur]);
      dp[1][cur] += penalty;
    }
    for (int cur = 0; cur < sz; cur++) {
      dp[0][cur] = dp[1][cur];
      dp[1][cur] = (long long)1e18;
    }
  }
  long long ans = (long long)1e18;
  for (int i = 0; i < sz; i++) ans = min(ans, dp[0][i]);
  cout << ans;
  return 0;
}
