//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <numeric>
#include <cmath>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <limits>
#include <limits.h>
#include <iomanip>
#include <algorithm>
using namespace std;

#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define int long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mkp make_pair
#define fi first
#define se second

struct node {
    int left, right;
};

void solve() {
    int N;
    cin >> N;
    vector<int> X(N), C(N);
    vector<struct node> pos(N+2), dp(N+2);

    for(int i = 0; i <= N+1; i++) {
        pos[i].left = INT_MAX;
        pos[i].right = INT_MIN;
    }

    unordered_set<int> st;
    for(int i = 0; i < N; i++) {
        cin >> X[i] >> C[i];
        st.insert(C[i]);
        pos[C[i]].left = min(pos[C[i]].left, X[i]);
        pos[C[i]].right = max(pos[C[i]].right, X[i]);
    }   
    st.insert(N+1);

    pos[N+1].left = pos[N+1].right = 0;
    pos[0].left = pos[0].right = 0;
    dp[0].left = dp[0].right = 0;
    for(int i = 1; i <= N+1; i++) {
        if(st.find(i) == st.end()) {
            pos[i].left = pos[i-1].left;
            pos[i].right = pos[i-1].right;
            dp[i].left = dp[i-1].left;
            dp[i].right = dp[i-1].right;
            continue;
        }
        dp[i].left = min(dp[i-1].left + abs(pos[i-1].left - pos[i].right) + abs(pos[i].right - pos[i].left),
                        dp[i-1].right + abs(pos[i-1].right - pos[i].right) + abs(pos[i].right - pos[i].left));
        dp[i].right = min(dp[i-1].left + abs(pos[i-1].left - pos[i].left) + abs(pos[i].right - pos[i].left),
                        dp[i-1].right + abs(pos[i-1].right - pos[i].left) + abs(pos[i].right - pos[i].left));
        //cout << imie(pos[i-1].left)  imie(pos[i-1].right) << '\n';
        //cout << imie(dp[i].left)  imie(dp[i].right) << '\n';
    }
   // cout << min(abs(pos[N].left) + dp[N].left,
           //     abs(pos[N].right) + dp[N].right);
    cout << min(dp[N+1].left, dp[N+1].right);
}

int32_t main() 
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    IOS;

    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define int ll
#define count_one(a) __builtin_popcountll(a)
#define is_power_of_2(a) (a) & (a - 1) == 0
#define exit return 0
const double eps = 1e-10;
const double pi = acos(-1.0);
#define endl "\n"
#define flush() cout << flush
#define IOS                                                                    \
    ios_base::sync_with_stdio(false);                                          \
    cin.tie(NULL);
using namespace std;
void dbg_out() {
    cout << endl;
}
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
    cout << ' ' << H;
    dbg_out(T...);
}

#ifndef ONLINE_JUDGE
#define dbg(...)                                                               \
    cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__), cout << endl
#else
#define dbg(...)
#endif

//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////

#define inf (int)2E5 + 1
int n;
// 0 - left end
// 1 - right end
int dp[inf][2];
vector<int> loc[inf];

int32_t main() {
    IOS;
    cin >> n;
    int x, c;
    for (int i = 0; i < n; i++) {
        cin >> x >> c;
        loc[c].push_back(x);
    }

    for (int i = 1; i <= n; i++) {
        sort(loc[i].begin(), loc[i].end());
    }

    int start;

    for (start = 1; start <= n; start++) {
        if (loc[start].size()) {
            break;
        }
    }

    int prev = start;
    int prevEnd = 0;
    int prevStart = 0;
    for (int i = start; i <= n; i++) {
        if (!loc[i].size()) {
            continue;
        }

        int currLen = loc[i].size();
        int currStart = loc[i][0];
        int currEnd = loc[i][currLen - 1];
        // left end
        dp[i][0] = min(
            {dp[prev][1] + (abs(prevEnd - currEnd) + abs(currEnd - currStart)),
             dp[prev][0] +
                 (abs(prevStart - currEnd) + abs(currEnd - currStart))});

        // right end
        dp[i][1] =
            min({dp[prev][1] +
                     (abs(prevEnd - currStart) + abs(currEnd - currStart)),
                 dp[prev][0] +
                     (abs(prevStart - currStart) + abs(currEnd - currStart))});
        dbg(i);
        dbg(dp[i][0]);
        dbg(dp[i][1]);
        prev = i;
        prevEnd = loc[i][currLen - 1];
        prevStart = loc[i][0];
    }

    int prevLen = loc[prev].size();

    int ans = min(dp[prev][0] + abs(prevStart), dp[prev][1] + abs(prevEnd));

    cout << ans << endl;
    return 0;
}
