#include <bits/stdc++.h>
using namespace std;
const string NAME = "575F";
const long long INF = 1e18 + 7;
const int N = 5e3 + 8;
int n, m, b[2 * N], l[N], r[N], x, a[2 * N], k = 0;
long long f[2 * N], g[2 * N];
void Enter() {
  cin >> n >> x;
  for (int i = (1), _b = (n); i <= _b; ++i) {
    cin >> l[i] >> r[i];
    b[i] = l[i];
    b[i + n] = r[i];
  }
  b[0] = x;
  sort(b, b + 1 + 2 * n);
  for (int i = (0), _b = (2 * n); i <= _b; ++i)
    if (i == 0 || b[i] != b[i - 1]) a[++k] = b[i];
}
inline void Minimize(long long &a, const long long &b) {
  if (a > b) a = b;
}
inline int Cost(int pos, int l, int r) {
  if (pos >= l && pos <= r) return 0;
  if (pos < l) return l - pos;
  if (pos > r) return pos - r;
}
void Process() {
  fill(f + 1, f + 1 + k, INF);
  fill(g + 1, g + 1 + k, INF);
  for (int i = (1), _b = (k); i <= _b; ++i)
    if (a[i] == x) f[i] = g[i] = 0;
  for (int z = (1), _b = (n + 1); z <= _b; ++z) {
    for (int i = (1), _b = (k); i <= _b; ++i) f[i] = g[i] = min(f[i], g[i]);
    if (z == n + 1) break;
    for (int i = (2), _b = (k); i <= _b; ++i) {
      Minimize(f[i], f[i - 1] + a[i] - a[i - 1]);
    }
    for (int i = (k - 1), _b = (1); i >= _b; --i)
      Minimize(g[i], g[i + 1] + a[i + 1] - a[i]);
    for (int i = (1), _b = (k); i <= _b; ++i) {
      f[i] += Cost(a[i], l[z], r[z]);
      g[i] += Cost(a[i], l[z], r[z]);
    }
  }
  cout << *min_element(f + 1, f + 1 + k) << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  Enter();
  Process();
}
