#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
  cout << x << endl;
  exit(0);
}
using ll = long long;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const ll inf = 1e18;
int n, x;
int l[maxn], r[maxn];
vector<int> ord;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> x;
  ord.push_back(x);
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    ord.push_back(l[i]);
    ord.push_back(r[i]);
  }
  sort(ord.begin(), ord.end());
  ord.erase(std::unique(ord.begin(), ord.end()), ord.end());
  int N = ord.size();
  vector<ll> dp(N, inf);
  for (int i = 0; i < N; i++) {
    dp[i] = abs(x - ord[i]);
  }
  for (int i = 0; i < n; i++) {
    vector<ll> nxt(N, inf);
    for (int j = 0; j < N; j++) {
      if (ord[j] < l[i]) {
        nxt[j] = dp[j] + (l[i] - ord[j]);
      } else if (ord[j] > r[i]) {
        nxt[j] = dp[j] + (ord[j] - r[i]);
      } else {
        nxt[j] = dp[j];
      }
    }
    if (i < n - 1) {
      for (int j = 1; j < N; j++) {
        nxt[j] = min(nxt[j], nxt[j - 1] + (ord[j] - ord[j - 1]));
      }
      for (int j = N - 2; j >= 0; j--) {
        nxt[j] = min(nxt[j], nxt[j + 1] + (ord[j + 1] - ord[j]));
      }
    }
    swap(nxt, dp);
  }
  cout << *min_element(dp.begin(), dp.end()) << endl;
  return 0;
}
