#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   bool comp(pair<long long, long long> p1, pair<long long, long long> p2) {     if (p1.first != p2.first) {       return p1.first < p2.first;     } else       return p2.second < p1.second;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n;     cin >> n;     map<long long, vector<pair<long long, long long>>> m;     for (long long i = 0; i < n; i++) {       long long x, y;       cin >> x >> y;       long long level = max(x, y);       m[level].push_back({x, y});     }     vector<pair<long long, long long>> v;     long long dp[200006][2];     memset(dp, INT_MAX, sizeof dp);     dp[0][0] = 0;     dp[0][1] = 0;     pair<long long, long long> p_start, p_end;     p_start = {0, 0};     p_start = {0, 0};     long long i = 1;     for (auto it : m) {       for (auto it1 : it.second) {         v.push_back(it1);       }       sort(v.begin(), v.end(), comp);       pair<long long, long long> start = v[0];       pair<long long, long long> end = v[v.size() - 1];       long long di =           abs(start.first - end.first) + abs(start.second - end.second);       long long a1 = dp[i - 1][0] + abs(p_start.first - end.first) +                      abs(p_start.second - end.second) + di;       long long a2 = dp[i - 1][1] + abs(p_end.first - end.first) +                      abs(p_end.second - end.second) + di;       dp[i][0] = min(a1, a2);       a1 = dp[i - 1][0] + abs(p_start.first - start.first) +            abs(p_start.second - start.second) + di;       a2 = dp[i - 1][1] + abs(p_end.first - start.first) +            abs(p_end.second - start.second) + di;       dp[i][1] = min(a1, a2);       p_start = start;       p_end = end;       v.clear();       i++;     }     cout << min(dp[i - 1][0], dp[i - 1][1]);     return 0;   }   