#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
const long long inf = 1e15;
long long dp[2][N][2];
int a[N][2];
void load() {}
void trungtt() {
  int n, x;
  scanf("%d%d", &n, &x);
  long long ans = 0;
  int l = x, r = x;
  for (int i = 1; i <= n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (l > b) {
      ans += l - b;
      r = l;
      l = b;
    } else if (r < a) {
      ans += a - r;
      l = r, r = a;
    } else {
      if (l < a) l = a;
      if (r > b) r = b;
    }
  }
  printf("%lld", ans);
  return;
}
int main() {
  load();
  trungtt();
  return 0;
}
