#include <bits/stdc++.h>
using namespace std;
const long long INF = ~0ull >> 2;
int n, x, m;
int l[5000], r[5000];
int sor[10001];
long long dp[2][10001];
inline void update(long long &a, long long b) {
  if (a > b) a = b;
}
int main() {
  scanf("%d%d", &n, &x);
  sor[m++] = x;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &l[i], &r[i]);
    sor[m++] = l[i];
    sor[m++] = r[i];
  }
  sort(sor, sor + m);
  m = unique(sor, sor + m) - sor;
  for (int i = 0; i < n; i++) {
    l[i] = lower_bound(sor, sor + m, l[i]) - sor;
    r[i] = lower_bound(sor, sor + m, r[i]) - sor;
  }
  x = lower_bound(sor, sor + m, x) - sor;
  for (int j = 0; j < m; j++) dp[0][j] = INF;
  dp[0][x] = 0;
  for (int i = 0; i < n; i++) {
    int t1 = i & 1, t2 = t1 ^ 1;
    for (int j = 0; j < m; j++) dp[t2][j] = INF;
    int p = 0;
    for (int j = 0; j < m; j++) {
      if (dp[t1][p] + sor[j] - sor[p] < dp[t1][j]) {
        update(dp[t2][j], dp[t1][p] + sor[j] - sor[p]);
      } else {
        update(dp[t2][j], dp[t1][j]);
        p = j;
      }
    }
    p = m - 1;
    for (int j = m - 1; j >= 0; j--) {
      if (dp[t1][p] + sor[p] - sor[j] < dp[t1][j]) {
        update(dp[t2][j], dp[t1][p] + sor[p] - sor[j]);
      } else {
        update(dp[t2][j], dp[t1][j]);
        p = j;
      }
    }
    for (int j = 0; j < l[i]; j++) dp[t2][j] += sor[l[i]] - sor[j];
    for (int j = r[i] + 1; j < m; j++) dp[t2][j] += sor[j] - sor[r[i]];
  }
  long long ans = INF;
  for (int j = 0; j < m; j++) update(ans, dp[n & 1][j]);
  printf("%I64d\n", ans);
  return 0;
}
