#include <bits/stdc++.h>
const int N = 5005;
const long long INF = (long long)1e18 + 228;
using namespace std;
int n, x, l[N], r[N];
long long pref[2 * N], cur[2 * N];
vector<int> p;
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]);
  for (int i = 1; i <= n; i++) p.push_back(l[i]), p.push_back(r[i]);
  p.push_back(x);
  sort(p.begin(), p.end());
  for (int j = 0; j < (int)p.size(); j++) pref[j] = INF;
  for (int i = 0; i < (int)p.size(); i++)
    if (p[i] == x) pref[i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)p.size(); j++) {
      cur[j] = pref[j];
      if (j > 0) cur[j] = min(cur[j], cur[j - 1] + p[j] - p[j - 1]);
    }
    for (int j = (int)p.size() - 1; j >= 0; j--) {
      if (j < (int)p.size() - 1)
        cur[j] = min(cur[j], cur[j + 1] + p[j + 1] - p[j]);
    }
    for (int j = 0; j < (int)p.size(); j++) {
      if (p[j] < l[i] || p[j] > r[i])
        cur[j] += min(abs(p[j] - l[i]), abs(p[j] - r[i]));
    }
    for (int j = 0; j < (int)p.size(); j++) pref[j] = cur[j];
  }
  long long ans = INF;
  for (int i = 0; i < (int)p.size(); i++) ans = min(ans, cur[i]);
  cout << ans << endl;
  return 0;
}
