func_code_string: #include <bits/stdc++.h>   using namespace std;   int l[5010], r[5010];   vector<int> vc;   long long f[2][10100];   int cost[10100];   int doit(int x, int y) {     if (vc[x] <= l[y]) return l[y] - vc[x];     if (vc[x] >= r[y]) return vc[x] - r[y];     return 0;   }   int main() {     int start = 0;     int n;     scanf( %d %d , &n, &start);     vc.push_back(start);     for (int i = 1; i <= n; ++i) {       scanf( %d %d , &l[i], &r[i]);       vc.push_back(l[i]);       vc.push_back(r[i]);     }     sort(vc.begin(), vc.end());     vc.erase(unique(vc.begin(), vc.end()), vc.end());     int now = 0, nxt = 1;     int cnt = vc.size();     for (int i = 0; i < cnt; ++i) {       cost[i] = doit(i, 1);     }     for (int i = 0; i < cnt; ++i) {       f[now][i] = abs(vc[i] - start) + cost[i];     }     for (int i = 1; i < n; ++i) {       memset(cost, 0, sizeof cost);       for (int j = 0; j < cnt; ++j) {         cost[j] = doit(j, i + 1);       }       int mn = f[now][cnt - 1], mx = cnt - 1;       for (int j = cnt - 1; j >= 0; --j) {         if (j < cnt - 1 && f[now][j] - (vc[cnt - 1] - vc[j]) < mn) {           mn = f[now][j] - (vc[cnt - 1] - vc[j]);           mx = j;         }         f[nxt][j] = f[now][mx] + vc[mx] - vc[j] + cost[j];       }       mn = f[now][0], mx = 0;       for (int j = 0; j < cnt; ++j) {         if (j && f[now][j] - (vc[j] - vc[0]) < mn) {           mn = f[now][j] - (vc[j] - vc[0]);           mx = j;         }         f[nxt][j] = min(f[nxt][j], f[now][mx] + vc[j] - vc[mx] + cost[j]);       }       swap(now, nxt);     }     long long ans = 1e18;     for (int i = 0; i < cnt; ++i) ans = min(ans, f[now][i]);     printf( %I64d n , ans);   }   