#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 10;
const long long MO = 1e9 + 7;
const long long IN = 1e18;
vector<int> vh;
long long dp[2][maxn * 2];
int a[2][maxn];
int n, x;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> x;
  vh.push_back(x);
  for (int i = 0; i < n; i++) {
    cin >> a[0][i] >> a[1][i];
    vh.push_back(a[0][i]);
    vh.push_back(a[1][i]);
  }
  sort((vh).begin(), (vh).end());
  vh.resize(unique((vh).begin(), (vh).end()) - vh.begin());
  fill(dp[0], dp[0] + maxn * 2, IN);
  fill(dp[1], dp[1] + maxn * 2, IN);
  int k = 0;
  for (int t = 0; t < ((int)(vh).size()); t++) dp[k][t] = abs(x - vh[t]);
  for (int i = 0; i < n; i++) {
    k = 1 - k;
    long long mn;
    mn = IN;
    for (int t = 0; t < ((int)(vh).size()); t++) {
      long long cur = ((vh[t] >= a[0][i] && vh[t] <= a[1][i])
                           ? 0
                           : min(abs(a[0][i] - vh[t]), abs(a[1][i] - vh[t])));
      if (t) mn += vh[t] - vh[t - 1];
      mn = min(mn, dp[1 - k][t]);
      dp[k][t] = mn + cur;
    }
    mn = IN;
    for (int t = ((int)(vh).size()) - 1; t >= 0; t--) {
      long long cur = ((vh[t] >= a[0][i] && vh[t] <= a[1][i])
                           ? 0
                           : min(abs(a[0][i] - vh[t]), abs(a[1][i] - vh[t])));
      if (t - ((int)(vh).size()) + 1) mn += vh[t + 1] - vh[t];
      mn = min(mn, dp[1 - k][t]);
      dp[k][t] = min(dp[k][t], mn + cur);
    }
  }
  long long ans = IN;
  for (int i = 0; i < maxn * 2; i++) ans = min(ans, dp[k][i]);
  cout << ans << "\n";
}
