#include <bits/stdc++.h>
using namespace std;
long long dp[2][5005 * 2];
pair<int, int> arr[5005];
int n;
unordered_map<int, int> rnk, revrnk;
set<int> numbers;
int nearest_spot(pair<int, int> cur, int place) {
  if (cur.first > cur.second) swap(cur.first, cur.second);
  if (cur.first <= place && place <= cur.second) return place;
  if (place < cur.first) return cur.first;
  return cur.second;
}
int move(int from, int to, int idx) {
  from = revrnk[from];
  to = revrnk[to];
  pair<int, int> cur = arr[idx];
  int curSpot = nearest_spot(arr[idx], from);
  return abs(from - curSpot) +
         abs(nearest_spot(make_pair(curSpot, from), to) - to);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int x;
  cin >> n >> x;
  numbers.insert(x);
  for (int i = 0; i < n; i++)
    cin >> arr[i].first >> arr[i].second, numbers.insert(arr[i].first),
        numbers.insert(arr[i].second);
  int lst = 0;
  for (set<int>::iterator it = numbers.begin(); it != numbers.end(); it++)
    rnk[*it] = ++lst, revrnk[lst] = *it;
  int cur = 0, prev = 1;
  for (int j = 1; j <= lst; j++)
    dp[cur][j] = abs(revrnk[j] - nearest_spot(arr[n - 1], revrnk[j]));
  for (int i = n - 2; i >= 0; i--) {
    swap(cur, prev);
    int s = rnk[arr[i].first], e = rnk[arr[i].second];
    for (int j = s; j <= e; j++) dp[cur][j] = dp[prev][j];
    for (int j = s - 1; j >= 1; j--)
      dp[cur][j] = min(dp[cur][j + 1] + revrnk[j + 1] - revrnk[j],
                       dp[prev][j] + move(j, j, i));
    for (int j = e + 1; j <= lst; j++)
      dp[cur][j] = min(dp[cur][j - 1] + revrnk[j] - revrnk[j - 1],
                       dp[prev][j] + move(j, j, i));
  }
  cout << dp[cur][rnk[x]] << '\n';
  return 0;
}
