#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool uin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
const int mod = 1000000000 + 7;
int mul(int x, int y) { return x * 1ll * y % mod; }
int power(int x, int n) {
  int r = 1;
  while (n) {
    if (n & 1) r = mul(r, x);
    if (n /= 2) x = mul(x, x);
  }
  return r;
}
int inv(int x) { return power(x, mod - 2); }
const int N = 10010;
const long long inf = 1e18 + 100500;
int n;
pair<int, int> in[N];
vector<int> pos;
long long d[2][N];
void solve() {
  n = 5000;
  cin >> n;
  int st = rand();
  cin >> st;
  pos.push_back(st);
  for (int i = 0; i < (int)(n); ++i) cin >> in[i].first >> in[i].second;
  for (int i = 0; i < (int)(n); ++i)
    pos.push_back(in[i].first), pos.push_back(in[i].second);
  sort((pos).begin(), (pos).end());
  pos.erase(unique((pos).begin(), (pos).end()), pos.end());
  st = lower_bound((pos).begin(), (pos).end(), st) - pos.begin();
  for (int i = 0; i < (int)(n); ++i) {
    in[i].first =
        lower_bound((pos).begin(), (pos).end(), in[i].first) - pos.begin();
    in[i].second =
        lower_bound((pos).begin(), (pos).end(), in[i].second) - pos.begin();
  }
  int m = pos.size();
  for (int i = 0; i < (int)(m); ++i) d[0][i] = inf;
  d[0][st] = 0;
  auto up = [&](long long* d) {
    for (int i = 0; i < (int)(m); ++i)
      if (i) {
        uin(d[i], d[i - 1] + pos[i] - pos[i - 1]);
      }
    for (int i = (int)(m - 1) - 1; i >= 0; --i) {
      uin(d[i], d[i + 1] + pos[i + 1] - pos[i]);
    }
  };
  up(d[0]);
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(in[i].first); ++j)
      d[(i + 1) & 1][j] = d[i & 1][j] + pos[in[i].first] - pos[j];
    for (int j = (int)(in[i].first); j <= (int)(in[i].second); ++j)
      d[(i + 1) & 1][j] = d[i & 1][j];
    for (int j = (int)(in[i].second + 1); j <= (int)(m - 1); ++j)
      d[(i + 1) & 1][j] = d[i & 1][j] + pos[j] - pos[in[i].second];
    up(d[(i + 1) & 1]);
  }
  cout << *min_element(d[n & 1], d[n & 1] + m) << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cout.precision(15);
  cerr << fixed;
  cerr.precision(15);
  solve();
  return 0;
}
