#include <bits/stdc++.h>
using namespace std;
int getint() {
  unsigned int c;
  int x = 0;
  while (((c = getchar()) - '0') >= 10) {
    if (c == '-') return -getint();
    if (!~c) exit(0);
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while (((c = getchar()) - '0') < 10);
  return x;
}
int getstr(char *s) {
  int c, n = 0;
  while ((c = getchar()) <= ' ') {
    if (!~c) exit(0);
  }
  do {
    s[n++] = c;
  } while ((c = getchar()) > ' ');
  s[n] = 0;
  return n;
}
template <class T>
inline bool chmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool chmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
const long long inf = 1LL << 62;
int n, m, x;
int ss[5555], es[5555];
vector<int> nums;
map<int, int> compress;
const unsigned long long BASE = 999999937;
template <class Value>
struct Map {
  static const int M = 260 * 1000000;
  static const int E = sizeof(unsigned long long) + sizeof(bool) +
                       sizeof(Value),
                   N = M / E;
  int n;
  unsigned long long h[N];
  bool u[N];
  Value v[N];
  Map() { clear(); }
  void clear() { n = 0, memset(u, 0, sizeof(u)); }
  int size() { return n; }
  bool set(unsigned long long x, Value value) {
    int i = x % N;
    for (; u[i]; ++i != N ? i = i : i = 0)
      if (h[i] == x) {
        v[i] = value;
        return 0;
      }
    h[i] = x, u[i] = 1, v[i] = value, n++;
    return 1;
  }
  bool get(unsigned long long x, Value &res) {
    for (int i = x % N; u[i]; ++i != N ?: i = 0)
      if (h[i] == x) {
        res = v[i];
        return 1;
      }
    return 0;
  }
  Value get(unsigned long long x) {
    for (int i = x % N; u[i]; ++i != N ? i = i : i = 0)
      if (h[i] == x) return v[i];
    return Value();
  }
  bool count(unsigned long long x) {
    for (int i = x % N; u[i]; ++i != N ? i = i : i = 0)
      if (h[i] == x) return 1;
    return 0;
  }
};
inline long long get_cost(int pos, int turn) {
  int s = ss[turn];
  int e = es[turn];
  if (pos < s) return nums[s] - nums[pos];
  if (pos > e) return nums[pos] - nums[e];
  return 0;
}
Map<long long> cache;
long long solve(int turn, int pos) {
  int key = pos * 5005 + turn;
  if (cache.count(key)) return cache.get(key);
  long long res;
  if (turn == n) return res = 0;
  res = inf;
  int srt = ss[turn], end = es[turn];
  if (srt <= pos && pos <= end) {
    res = solve(turn + 1, pos);
  } else {
    int lo = 0, hi = m - 1;
    while (lo < hi) {
      int mid = (lo + hi) >> 1;
      long long mv_cost1 = abs(nums[mid] - nums[pos]);
      long long mv_cost2 = abs(nums[mid + 1] - nums[pos]);
      long long cr_cost1 = get_cost(mid, turn);
      long long cr_cost2 = get_cost(mid + 1, turn);
      long long nx_cost1 = solve(turn + 1, mid);
      long long nx_cost2 = solve(turn + 1, mid + 1);
      long long cost1 = mv_cost1 + cr_cost1 + nx_cost1;
      long long cost2 = mv_cost2 + cr_cost2 + nx_cost2;
      if (cost1 < cost2) {
        chmin(res, cost1);
        hi = mid;
      } else {
        lo = mid + 1;
        chmin(res, cost2);
      }
    }
  }
  cache.set(key, res);
  return res;
}
int main() {
  int i, j, tcc, tc = 1 << 28;
  for (tcc = 0; tcc < tc; tcc++) {
    n = getint(), x = getint() - 1;
    nums.push_back(x);
    for (i = 0; i < n; i++) {
      ss[i] = getint() - 1;
      es[i] = getint() - 1;
      nums.push_back(ss[i]);
      nums.push_back(es[i]);
    }
    sort(nums.begin(), nums.end());
    nums.erase(unique(nums.begin(), nums.end()), nums.end());
    m = nums.size();
    for (i = 0; i < nums.size(); i++) compress[nums[i]] = i;
    x = compress[x];
    for (i = 0; i < n; i++) {
      ss[i] = compress[ss[i]];
      es[i] = compress[es[i]];
    }
    compress.clear();
    long long res = solve(0, x);
    cout << res << endl;
  }
  return 0;
}
