#include <bits/stdc++.h>
using namespace std;
struct node {
  int l, r;
};
int n, m, s;
int rev[5005 * 2];
node p[5005];
long long t[5005 * 2];
long long mem[5005 * 2];
map<int, int> mp;
int len(int x, int l, int r) {
  if (l <= x && x <= r) return 0;
  if (x < l) return l - x;
  return x - r;
}
int main() {
  int i, x;
  long long now, ans;
  map<int, int>::iterator it;
  scanf("%d%d", &n, &s);
  for (i = 1; i <= n; i++) scanf("%d%d", &p[i].l, &p[i].r);
  mp[s];
  for (i = 1; i <= n; i++) mp[p[i].l], mp[p[i].r];
  for (it = mp.begin(), m = 1; it != mp.end(); it++, m++) {
    it->second = m;
    rev[m] = it->first;
  }
  for (i = 1; i < m; i++) mem[i] = (long long)1e15;
  mem[mp[s]] = 0;
  for (i = 1; i <= n; i++) {
    for (x = 1; x < m; x++) t[x] = (long long)1e15;
    now = (long long)1e15;
    for (x = 1; x < m; x++) {
      now = min(now, mem[x]);
      t[x] = min(t[x], now);
      now += rev[x + 1] - rev[x];
    }
    now = (long long)1e15;
    for (x = m - 1; x >= 1; x--) {
      now = min(now, mem[x]);
      t[x] = min(t[x], now);
      now += rev[x] - rev[x - 1];
    }
    for (x = 1; x < m; x++) mem[x] = t[x] + len(rev[x], p[i].l, p[i].r);
  }
  ans = (long long)1e15;
  for (i = 1; i < m; i++) ans = min(ans, mem[i]);
  printf("%I64d", ans);
}
