#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
const long long INF = 1e15;
set<int> S;
long long l[maxn], r[maxn];
long long hs[maxn];
long long dist[maxn], dp[maxn];
int n, m, x;
int main() {
  scanf("%d%d", &n, &x);
  S.insert(x);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &l[i], &r[i]);
    S.insert(l[i]);
    S.insert(r[i]);
  }
  for (set<int>::iterator it = S.begin(); it != S.end(); it++) {
    hs[++m] = *it;
    dist[m] = hs[m] - hs[m - 1];
  }
  for (int i = 1; i <= m; i++) dp[i] = (hs[i] == x ? 0 : INF);
  for (int i = 1; i <= n; i++) {
    for (int j = 2; j <= m; j++) dp[j] = min(dp[j], dp[j - 1] + dist[j]);
    for (int j = m - 1; j >= 1; j--)
      dp[j] = min(dp[j], dp[j + 1] + dist[j + 1]);
    for (int j = 1; j <= m; j++) {
      if (hs[j] >= l[i] && hs[j] <= r[i]) continue;
      dp[j] += min(abs(hs[j] - l[i]), abs(hs[j] - r[i]));
    }
  }
  long long ans = INF;
  for (int i = 1; i <= m; i++) ans = min(ans, dp[i]);
  printf("%lld\n", ans);
  return 0;
}
