#include <bits/stdc++.h>   using namespace std;   long long dp[10010], leftx[10010], rightx[10010], l[5010], r[5010];   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int n, x;     cin >> n >> x;     vector<int> ry;     for (int i = 0; i < n; i++) {       int a, b;       cin >> a >> b;       l[i] = a;       r[i] = b;       ry.push_back(a);       ry.push_back(b);     }     ry.push_back(x);     sort(ry.begin(), ry.end());     set<int> rt;     for (auto val : ry) {       rt.insert(val);     }     ry.clear();     for (auto val : rt) {       ry.push_back(val);     }     int ind = -1;     for (int i = 0; i < ry.size(); i++) {       if (ry[i] == x) ind = i;     }     for (int i = 0; i < ry.size(); i++) dp[i] = 1e18;     for (int i = 0; i < ry.size(); i++) {       if (ry[i] == x) dp[i] = 0;     }     for (int i = 0; i <= n; i++) {       if (i) {         for (int j = 0; j < ry.size(); j++) {           long long val = 0;           if (ry[j] < l[i - 1] || ry[j] > r[i - 1])             val = min(abs(l[i - 1] - ry[j]), abs(r[i - 1] - ry[j]));           dp[j] = min(leftx[j] + ry[j] + val, rightx[j] - ry[j] + val);         }       }       leftx[0] = dp[0] - ry[0];       for (int j = 1; j < ry.size(); j++)         leftx[j] = min(leftx[j - 1], dp[j] - ry[j]);       rightx[ry.size() - 1] = dp[ry.size() - 1] + ry[ry.size() - 1];       for (int j = (int)ry.size() - 2; j >= 0; j--)         rightx[j] = min(rightx[j + 1], dp[j] + ry[j]);     }     long long mini = 1e18;     for (int j = 0; j < ry.size(); j++) mini = min(mini, dp[j]);     cout << mini <<   n ;   }   