#include <bits/stdc++.h>
using namespace std;
long long dp[2][10010][3];
vector<int> all;
int l[5010];
int r[5010];
int gl[5010];
int gr[5010];
const int magic = 3;
int main() {
  int n, x;
  scanf("%d%d", &n, &x);
  all.push_back(x);
  for (int i = (0); i < (n); ++i) {
    scanf("%d%d", &l[i], &r[i]);
    all.push_back(l[i]);
    all.push_back(r[i]);
  }
  sort(all.begin(), all.end());
  all.resize(unique(all.begin(), all.end()) - all.begin());
  for (int i = (0); i < (n); ++i) {
    gl[i] = lower_bound(all.begin(), all.end(), l[i]) - all.begin();
    gr[i] = lower_bound(all.begin(), all.end(), r[i]) - all.begin();
  }
  int v = 0;
  for (int i = n - 1; i >= 0; --i) {
    int nv = (v ^ 1);
    for (int j = (0); j < (all.size()); ++j) {
      int cost = 0;
      if (all[j] < l[i]) cost = l[i] - all[j];
      if (all[j] > r[i]) cost = all[j] - r[i];
      dp[v][j][1] = dp[nv][j][0] + cost;
      if (j)
        dp[v][j][1] = min(dp[v][j][1], all[j] - all[j - 1] + dp[v][j - 1][1]);
    }
    for (int j = (int)all.size() - 1; j >= 0; --j) {
      int cost = 0;
      if (all[j] < l[i]) cost = l[i] - all[j];
      if (all[j] > r[i]) cost = all[j] - r[i];
      dp[v][j][2] = dp[nv][j][0] + cost;
      if (j + 1 != all.size())
        dp[v][j][2] = min(dp[v][j][2], all[j + 1] - all[j] + dp[v][j + 1][2]);
    }
    for (int j = (0); j < (all.size()); ++j) {
      dp[v][j][0] = min(dp[v][j][1], dp[v][j][2]);
    }
    v ^= 1;
  }
  int gx = lower_bound(all.begin(), all.end(), x) - all.begin();
  cout << dp[v ^ 1][gx][0] << endl;
  return 0;
}
