#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <functional>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <queue>
#include <bitset>
#include <cassert>
#include <math.h>
#include <complex>
#include <iomanip>
#include <unordered_map>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#ifdef _OLIMPOLOCAL
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
    #define debug(...) 1337
#endif
void debug_out() { cout << "\n";}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cout << " " << A; debug_out(B...); }
int test = 1;
#define out(x) {cout << x << "\n";return;}
#define all(N) N.begin(),N.end()
#define allr(N) N.rbegin(),N.rend()
template<typename T1> 
void pr(vector<T1> V, int add = 0, int start = -1, int end = -1) {
    if (start < 0) start = 0;
    if (end < 0) end = V.size() - 1;
    for (int i = start; i <= end; i++) {
        cout << V[i] + add << ((i == end) ? "\n" : " ");
    }
}
template<typename T1> 
T1 chmin(T1 &x, const T1 v) { return x = min(x,v); }
template<typename T1> 
T1 chmax(T1 &x, const T1 v) { return x = max(x,v); }

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define repr(i, n) for (int i = n-1; i >= 0; i--)
#define repsr(i, s, n) for (int i = n-1; i >= s; i--)
#define PI pair<int,int>
template<typename T1> 
T1 gcd(const T1 &a, const T1 &b) {
    if (a == 0 || b == 0) return a + b;
    return gcd(b, a %b);
}
//-------------------------- ^ DONT TOUCH ^-----------------------------------------------------------------
#define MAX 200001 
#define MOD 998244353

ll C1[MAX], C2[MAX];
ll D1[MAX], D2[MAX];
bool HC[MAX];
ll n, x, c;
void solve() {
    cin >> n;
    rep(i,n) {
        cin >> x >> c;
        if (!HC[c]) {
            C1[c] = C2[c] = x;
        }
        HC[c] = 1;
        chmin(C1[c], x);
        chmax(C2[c], x);
    }
    C1[0] = C2[0] = D1[0] = D2[0] = 0;
    reps(i, 1, n+1) {
        if (!HC[i]) {
            C1[i] = C1[i-1];
            C2[i] = C2[i-1];
            D1[i] = D1[i-1];
            D2[i] = D2[i-1];
            continue;
        }

        D1[i] = min(D1[i-1] + abs(C2[i] - C1[i-1]), 
                    D2[i-1] + abs(C2[i] - C2[i-1])) + abs(C1[i] - C2[i]);
        D2[i] = min(D1[i-1] + abs(C1[i] - C1[i-1]), 
                    D2[i-1] + abs(C1[i] - C2[i-1])) + abs(C1[i] - C2[i]);
        //debug(i, C1[i], C2[i], D1[i], D2[i]);
    }
    out(min(D1[n] + abs(C1[n]), D2[n] + abs(C2[n])))
}
 
int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);
    int test = 1;
#ifdef _OLIMPOLOCAL
    cin >> test;
#endif
    rep(testCase, test) {
        solve();
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
const int INF = 2e9 + 5;
const int dpINF = 1e16;
const int off = 1e9;
void print(vector<pair<int, int>> a) {
    cout << "\n";
    for(auto i : a) {
        cout << i.first << " " << i.second << "\n";
    }
    cout << "\n";
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);

    for(int i = 0; i < n; i++) {
        cin >> a[i].first >> a[i].second;
//        a[i].first += off;
    }
    sort(a.begin(), a.end(), [](pair<int, int> l, pair<int, int> r) { return l.second < r.second; });

    vector<pair<int, int>> v(n+2, {INF, -INF});
    v[0] = {0, 0};
    v[n+1] = {0, 0};
    for(int i = 0; i < n; i++) {
        v[a[i].second].first = min(v[a[i].second].first, a[i].first);
        v[a[i].second].second = max(v[a[i].second].second, a[i].first);
    }
//    print(v);
//    print(a);

    int dp[n+5][2];
    for(int i = 0; i <= n+4; i++) {
        for(int j = 0; j < 2; j++) {
            dp[i][j] = dpINF;
        }
    }
    dp[0][0] = dp[0][1] = 0;

    cerr << "\n";
    for(int i = 1; i <= n+1; i++) {
        if (v[i].first == INF) {
            v[i] = v[i-1];
            dp[i][0] = dp[i-1][0];
            dp[i][1] = dp[i-1][1];
            continue;
        }
        dp[i][0] = min(dp[i][0], dp[i-1][0] + abs(v[i-1].first - v[i].second) + abs(v[i].second - v[i].first));
        dp[i][0] = min(dp[i][0], dp[i-1][1] + abs(v[i-1].second - v[i].second) + abs(v[i].second - v[i].first));
        dp[i][1] = min(dp[i][1], dp[i-1][0] + abs(v[i-1].first - v[i].first) + abs(v[i].second - v[i].first));
        dp[i][1] = min(dp[i][1], dp[i-1][1] + abs(v[i-1].second - v[i].first) + abs(v[i].second - v[i].first));
//        cout << dp[i][0] << " " << dp[i][1] << "\n";
    }

    int ans = min(dp[n+1][0], dp[n+1][1]);
    cout << ans;
    return 0;
}
