#include <bits/stdc++.h>
using namespace std;
int dis(int a, int b, int x) {
  if (x > b) return x - b;
  if (x < a) return a - x;
  return 0;
}
int main() {
  int n, x;
  scanf("%d %d", &n, &x);
  vector<pair<int, int> > len(n);
  vector<int> v;
  v.push_back(x);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &len[i].first, &len[i].second);
    v.push_back(len[i].first);
    v.push_back(len[i].second);
  }
  sort(v.begin(), v.end());
  long long dp[10005];
  long long l[10005];
  long long r[10005];
  for (int i = 0; i < v.size(); i++) {
    dp[i] = dis(len[n - 1].first, len[n - 1].second, v[i]);
    l[i] = dp[i] - v[i];
    r[i] = dp[i] + v[i];
    if (i) l[i] = min(l[i], l[i - 1]);
  }
  for (int i = v.size() - 2; i >= 0; i--) {
    r[i] = min(r[i], r[i + 1]);
  }
  for (int i = n - 2; i >= 0; i--) {
    for (int j = 0; j < v.size(); j++) {
      dp[j] = dis(len[i].first, len[i].second, v[j]) +
              min(l[j] + v[j], r[j] - v[j]);
      l[j] = dp[j] - v[j];
      r[j] = dp[j] + v[j];
      if (j) l[j] = min(l[j], l[j - 1]);
    }
    for (int j = v.size() - 2; j >= 0; j--) {
      r[j] = min(r[j], r[j + 1]);
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == x) {
      cout << min(l[i] + v[i], r[i] - v[i]) << endl;
      return 0;
    }
  }
}
