#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e4;
int n;
long long f[2][N];
pair<int, int> p[N];
int l[N], s;
const long long oo = (long long)1e15;
void input() {
  cin >> n >> s;
  l[0] = s;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].first >> p[i].second;
    l[i] = p[i].first;
    l[i + n] = p[i].second;
  }
}
void opmin(long long &tar, long long v) {
  if (v < tar) tar = v;
}
long long dist(int x, int k) {
  if (l[x] < p[k].first) return p[k].first - l[x];
  if (l[x] > p[k].second) return l[x] - p[k].second;
  return 0;
}
void solve() {
  sort(l + 0, l + 1 + 2 * n);
  int nn = 1;
  l[nn] = l[0];
  for (int i = 1; i <= 2 * n; i++)
    if (l[i] != l[i - 1]) l[++nn] = l[i];
  int now = 0;
  for (int i = 1; i <= nn; i++)
    if (l[i] == s)
      f[now][i] = 0;
    else
      f[now][i] = oo;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= nn; j++) f[1 - now][j] = oo;
    long long minn = oo;
    for (int j = 1; j <= nn; j++) {
      minn = min(minn + l[j] - l[j - 1], f[now][j]);
      opmin(f[1 - now][j], minn + dist(j, i));
    }
    minn = oo;
    for (int j = nn; j >= 1; j--) {
      minn = min(minn + l[j + 1] - l[j], f[now][j]);
      opmin(f[1 - now][j], minn + dist(j, i));
    }
    now = 1 - now;
  }
  long long res = oo;
  for (int i = 1; i <= nn; i++) opmin(res, f[now][i]);
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  input();
  solve();
  return 0;
}
