#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 10;
const long long inf = 1e18;
long long dp[2][2 * maxn], r[2 * maxn], l[2 * maxn], n, x, m;
pair<long long, long long> a[maxn];
vector<long long> v;
void update(int idx) {
  r[m - 1] = dp[idx][m - 1];
  for (int i = m - 2; i >= 0; i--)
    r[i] = min(dp[idx][i], r[i + 1] + v[i + 1] - v[i]);
  l[0] = dp[idx][0];
  for (int i = 1; i < m; i++)
    l[i] = min(dp[idx][i], l[i - 1] + v[i] - v[i - 1]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> x;
  v.push_back(x);
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
    v.push_back(a[i].first);
    v.push_back(a[i].second);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  int pos = lower_bound(v.begin(), v.end(), x) - v.begin();
  m = v.size();
  for (int i = 0; i <= 1; i++)
    for (int j = 0; j <= m; j++) dp[i][j] = inf;
  dp[0][pos] = 0;
  update(0);
  for (int i = 1; i <= n; i++) {
    bool cur = (i % 2);
    for (int j = 0; j < m; j++) {
      if (v[j] >= a[i].first && v[j] <= a[i].second)
        dp[cur][j] = 0;
      else
        dp[cur][j] = min(abs(v[j] - a[i].first), abs(v[j] - a[i].second));
      long long add = min(l[j], r[j]);
      dp[cur][j] += add;
    }
    update(cur);
  }
  long long ans = inf;
  for (int i = 0; i < m; i++) ans = min(ans, dp[n % 2][i]);
  cout << ans << endl;
  return 0;
}
