#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9;
const int MAXN = 5e3 + 10;
const long long INF = 1e18;
long long dp[2][2 * MAXN];
vector<int> points;
int n, x;
vector<pair<int, int> > segs;
int main() {
  cin >> n >> x;
  points.push_back(x);
  for (int i = 0; i < (int)n; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    segs.push_back(make_pair(l, r));
    points.push_back(l);
    points.push_back(r);
  }
  sort(begin(points), end(points));
  points.resize(unique(begin(points), end(points)) - points.begin());
  int c = 0;
  for (int j = 0; j < (int)((int)(points).size()); ++j) {
    dp[c][j] = 0;
  }
  c ^= 1;
  for (int i = ((int)n) - 1; i >= 0; --i) {
    int l, r;
    tie(l, r) = segs[i];
    int pos = lower_bound(begin(points), end(points), l) - points.begin();
    for (int j = pos; j < ((int)(points).size()) && points[j] <= r; ++j) {
      dp[c][j] = dp[c ^ 1][j];
    }
    long long mn = INF;
    for (int j = pos; j >= 0; --j) {
      mn = min(mn, dp[c ^ 1][j]);
      dp[c][j] = (l - points[j]) + mn;
    }
    pos = lower_bound(begin(points), end(points), r) - points.begin();
    mn = INF;
    for (int j = pos; j < ((int)(points).size()); ++j) {
      mn = min(mn, dp[c ^ 1][j]);
      dp[c][j] = (points[j] - r) + mn;
    }
    c ^= 1;
  }
  cout << dp[c ^ 1][lower_bound(begin(points), end(points), x) - points.begin()]
       << endl;
  return 0;
}
