#include <bits/stdc++.h>
using namespace std;
const int N = 10500;
const long long INF = 1e15;
vector<long long> d, a;
vector<int> poses;
inline long long dist(long long x, long long l, long long r) {
  if (l <= x && x <= r) return 0;
  return min(abs(x - l), abs(x - r));
}
struct deq {
  int b, e;
  long long a[N];
  deq() {}
  bool empty() { return b == e; }
  long long back() { return a[e - 1]; }
  long long front() { return a[b]; }
  void push_back(long long c) { a[e++] = c; }
  void pop_back() { --e; }
  void pop_front() { ++b; }
  void clear() {
    b = 0;
    e = 0;
  }
};
deq q;
inline void make_q(int l, int r, int h) {
  for (int i = l; i < r; ++i) {
    long long c = a[i] + h * poses[i];
    while (!q.empty() && q.back() > c) {
      q.pop_back();
    }
    q.push_back(c);
  }
}
int main() {
  int n, x;
  cin >> n >> x;
  poses.push_back(x);
  vector<pair<int, int> > turn;
  for (int i = 0; i < n; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    poses.push_back(l);
    poses.push_back(r);
    turn.emplace_back(l, r);
  }
  sort(poses.begin(), poses.end());
  poses.resize(unique(poses.begin(), poses.end()) - poses.begin());
  int k = poses.size();
  d.resize(k);
  for (int i = 0; i < k; ++i) {
    if (poses[i] != x)
      d[i] = INF;
    else
      d[i] = 0;
  }
  for (int i = 0; i < n; ++i) {
    a = d;
    q.clear();
    make_q(0, k, 1);
    for (int j = 0; j < k; ++j) {
      d[j] = q.front() - poses[j];
      if (q.front() == a[j] + poses[j]) {
        q.pop_front();
      }
    }
    q.clear();
    for (int j = 0; j < k; ++j) {
      long long c = a[j] - poses[j];
      while (!q.empty() && q.back() > c) {
        q.pop_back();
      }
      q.push_back(c);
      d[j] = min(d[j], q.front() + poses[j]) +
             dist(poses[j], turn[i].first, turn[i].second);
    }
  }
  long long ans = INF;
  for (int i = 0; i < k; ++i) {
    ans = min(ans, d[i]);
  }
  cout << ans << endl;
  return 0;
}
