#include <bits/stdc++.h>
using namespace std;
void deba(int* a, int n) {
  cerr << "| ";
  for (int i = 0; i < (n); i++) cerr << a[i] << " ";
  cerr << "|" << endl;
}
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 30;
long long a[11000];
long long l[5001][2];
vector<int> p;
int main() {
  int n;
  scanf("%d", &n);
  ;
  int init;
  scanf("%d", &init);
  ;
  p.push_back(init);
  for (int i = 0; i < (n); i++) {
    scanf("%d %d", &l[i][0], &l[i][1]);
    ;
    p.push_back(l[i][0]);
    p.push_back(l[i][1]);
  }
  sort(p.begin(), p.end());
  long long len = n + n + 1;
  for (int i = 0; i < (len); i++) a[i] = abs(init - p[i]);
  for (int i = (0); i <= (n - 1); i++) {
    long long mn = 0;
    for (int j = 0; j < (len); j++) {
      if (a[j] < a[mn]) mn = j;
    }
    long long mm = mn;
    for (int j = (mn + 1); j <= (len - 1); j++) {
      if (a[mm] + (p[j] - p[mm]) > a[j])
        mm = j;
      else
        a[j] = a[mm] + (p[j] - p[mm]);
    }
    mm = mn;
    for (int j = (mn - 1); j >= (0); j--) {
      if (a[mm] + (p[mm] - p[j]) > a[j])
        mm = j;
      else
        a[j] = a[mm] + (p[mm] - p[j]);
    }
    for (int j = 0; j < (len); j++) {
      if (p[j] < l[i][0]) a[j] += l[i][0] - p[j];
      if (p[j] > l[i][1]) a[j] += p[j] - l[i][1];
    }
  }
  long long mn = 0;
  for (int j = 0; j < (len); j++) {
    if (a[j] < a[mn]) mn = j;
  }
  cout << a[mn] << endl;
  return 0;
}
