#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000")
using namespace std;
long long dp[10010];
int mas[5005][2];
set<int> st;
int pos[10010];
int main() {
  int n;
  scanf("%d", &n);
  int x;
  scanf("%d", &x);
  st.insert(x);
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d %d", &mas[i][0], &mas[i][1]);
    st.insert(mas[i][0]);
    st.insert(mas[i][1]);
  }
  int idx = 0;
  for (set<int>::iterator it = st.begin(); it != st.end(); it++)
    pos[idx++] = *it;
  int start = 0;
  for (int i = 0; i < idx; i++) {
    if (pos[i] == x) start = i;
  }
  int cur = 0, nxt = 1;
  dp[start] = 0;
  for (int i = start - 1; i >= 0; i--) {
    dp[i] = abs(pos[i] - pos[start]);
  }
  for (int i = start + 1; i < idx; i++) {
    dp[i] = abs(pos[i] - pos[start]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < idx; j++) {
      if (pos[j] < mas[i][0])
        dp[j] += (long long)(mas[i][0] - pos[j]);
      else if (pos[j] > mas[i][1])
        dp[j] += (long long)(pos[j] - mas[i][1]);
    }
    for (int j = 1; j < idx; j++) {
      dp[j] = min(dp[j], dp[j - 1] + (long long)abs(pos[j] - pos[j - 1]));
    }
    for (int j = idx - 2; j >= 0; j--) {
      dp[j] = min(dp[j], dp[j + 1] + (long long)abs(pos[j] - pos[j + 1]));
    }
  }
  long long res = 10000000000000000LL;
  for (int i = 0; i < idx; i++) res = min(res, dp[i]);
  cout << res << endl;
  return 0;
}
