#include <bits/stdc++.h>
using namespace std;
int l[5005], r[5005];
long long dp[2][10005], p, ans;
set<int> ms;
set<int>::iterator it;
int ind[100005];
int main() {
  int n, x, i, z, Z, m, j;
  scanf("%d%d", &n, &x);
  for (i = 0; i < n; i++) {
    scanf("%d%d", &l[i], &r[i]);
    ms.insert(l[i]);
    ms.insert(r[i]);
  }
  ms.insert(x);
  z = 0, Z = 1;
  for (it = ms.begin(), i = 0; it != ms.end(); it++, i++) {
    ind[i] = *it;
    dp[Z][i] = fabs(x - *it);
  }
  m = i;
  for (i = 0; i < n; i++, swap(z, Z)) {
    dp[z][0] = dp[Z][0];
    x = ind[0];
    p = dp[z][0];
    for (j = 1; j < m; j++) {
      if (fabs(x - ind[j]) + p > dp[Z][j]) {
        x = ind[j];
        p = dp[Z][j];
      }
      dp[z][j] = p + fabs(x - ind[j]);
    }
    dp[z][m - 1] = min(dp[Z][m - 1], dp[z][m - 1]);
    x = ind[m - 1];
    p = dp[z][m - 1];
    for (j = m - 2; j >= 0; j--) {
      if (fabs(x - ind[j]) + p > dp[Z][j]) {
        x = ind[j];
        p = dp[Z][j];
      }
      dp[z][j] = min(p + (long long)fabs(x - ind[j]), dp[z][j]);
    }
    for (j = 0; j < m; j++) {
      if (l[i] <= ind[j] && ind[j] <= r[i]) continue;
      if (ind[j] < l[i])
        dp[z][j] += l[i] - ind[j];
      else
        dp[z][j] += ind[j] - r[i];
    }
  }
  ans = dp[Z][0];
  for (i = 0; i < m; i++) ans = min(dp[Z][i], ans);
  printf("%lld\n", ans);
  return 0;
}
