#include <bits/stdc++.h>
using namespace std;
long long ret = 0;
int main() {
  int n, l, r, x, y;
  cin >> n >> y;
  x = y;
  for (int i = 0; i < n; ++i) {
    cin >> l >> r;
    if (l <= x && y <= r)
      x = min(x, r), y = max(y, l);
    else if (r < y)
      x = y, ret += y - r, y = r;
    else if (x < l)
      y = x, ret += l - x, x = l;
  }
  cout << ret << endl;
}
