#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
inline long long read() {
  int X = 0;
  bool flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    X = (X << 1) + (X << 3) + ch - '0';
    ch = getchar();
  }
  if (flag) return X;
  return ~(X - 1);
}
inline void write(long long x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
long long res(long long x) {
  long long ans = 1;
  for (long long i = 1; i <= x; ++i) {
    ans *= i;
  }
  return ans;
}
long long ans;
long long dp[N];
pair<int, int> a[N];
vector<int> vt;
int calc(int x, int p) {
  if (x < a[p].first) return a[p].first - x;
  if (x > a[p].second) return x - a[p].second;
  return 0;
}
int main() {
  int n = read(), m = read();
  vt.push_back(m);
  for (int i = 1; i <= n; ++i) {
    a[i].first = read(), a[i].second = read();
    vt.push_back(a[i].first);
    vt.push_back(a[i].second);
  }
  sort(vt.begin(), vt.end());
  vt.erase(unique(vt.begin(), vt.end()), vt.end());
  for (int i = 0; i < vt.size(); ++i) {
    if (vt[i] == m)
      dp[i] = 0;
    else
      dp[i] = 1e13;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < vt.size(); j++)
      dp[j] = min(dp[j], dp[j - 1] + vt[j] - vt[j - 1]);
    for (int j = vt.size() - 2; j >= 0; j--)
      dp[j] = min(dp[j], dp[j + 1] + vt[j + 1] - vt[j]);
    for (int j = 0; j < vt.size(); j++) dp[j] += calc(vt[j], i);
  }
  long long ans = 1e13;
  for (int i = 0; i < vt.size(); i++) ans = min(ans, dp[i]);
  write(ans);
  return 0;
}
