#include <bits/stdc++.h>
using namespace std;
void task();
int main() {
  srand(time(0));
  task();
  return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
const int M = 1e5 + 10;
int n, x;
int l[N];
int r[N];
long long int dp[2 * N + 10];
vector<int> v;
void task() {
  scanf("%d %d", &n, &x);
  v.push_back(x);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &l[i], &r[i]);
    v.push_back(l[i]);
    v.push_back(r[i]);
  }
  sort((v.begin()), (v.end()));
  int m = ((int)((v).size()));
  memset(dp, 0x3f, sizeof(dp));
  for (int i = 0; i < m; ++i)
    if (v[i] == x) dp[i] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j < m; ++j) dp[j] = min(dp[j], dp[j - 1] + v[j] - v[j - 1]);
    for (int j = m - 2; j >= 0; --j)
      dp[j] = min(dp[j], dp[j + 1] + v[j + 1] - v[j]);
    for (int j = 0; j < m; ++j)
      if (v[j] < l[i])
        dp[j] += l[i] - v[j];
      else if (v[j] > r[i])
        dp[j] += v[j] - r[i];
  }
  cout << *min_element(dp, dp + m);
}
