#include <bits/stdc++.h>
using namespace std;
vector<int> pos;
int x[5001], y[5001];
long long cst[10001], ncst[10001];
int n, k;
int cost(int l, int r, int xx) {
  if (xx >= l && xx <= r)
    return 0;
  else
    return min(abs(xx - l), abs(xx - r));
}
int main() {
  cin >> n >> k;
  pos.push_back(k);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    pos.push_back(x[i]);
    pos.push_back(y[i]);
  }
  sort(pos.begin(), pos.end());
  for (int i = 0; i <= n * 2; i++) {
    cst[i] = abs(pos[i] - k);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < pos.size(); j++) {
      ncst[j] = cst[j] + cost(x[i], y[i], pos[j]);
    }
    for (int j = 0; j + 1 < pos.size(); j++) {
      if (pos[j] < x[i] && ncst[j] < ncst[j + 1]) ncst[j + 1] = ncst[j];
    }
    for (int j = (int)(pos.size()) - 1; j >= 0; j--) {
      if (pos[j] > y[i] && ncst[j] < ncst[j - 1]) ncst[j - 1] = ncst[j];
    }
    for (int j = 0; j <= n * 2; j++) {
      cst[j] = ncst[j];
    }
  }
  long long ans = cst[0];
  for (int i = 1; i <= n * 2; i++) ans = min(cst[i], ans);
  cout << ans;
  return 0;
}
