#include<bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define pii pair<int,int>

int N;
vector<pii> V, X;

signed main()
{
    cin >> N;
    rep(i, N)
    {
        int x, c;
        cin >> x >> c;
        V.push_back({c, x});
    }

    sort(V.begin(), V.end());

    pii now = {0, 0};
    X.push_back(now);
    rep(i, N)
    {
        if(i == 0 || V[i - 1].first != V[i].first)now.first = V[i].second;
        if(i == N - 1 || V[i].first != V[i + 1].first)
        {
            now.second = V[i].second;
            X.push_back(now);
        }
    }
    X.push_back({0, 0});

    int l = 0, r = 0;
    for(int i = 0; i < X.size() - 1; i++)
    {
        pii now = X[i], next = X[i + 1];

        int nl = min(l + abs(now.first - next.second) + abs(next.first - next.second),
                     r + abs(now.second - next.second) + abs(next.first - next.second));
                
        int nr = min(l + abs(now.first - next.first) + abs(next.first - next.second),
                     r + abs(now.second - next.first) + abs(next.first - next.second));

        l = nl, r = nr;
    }

    cout << min(l, r) << endl;

    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define vi vector<int>
#define pi pair<int, int>
#define pl pair<ll, ll>
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
const int MOD = 1e9+ 7;
const int INF = numeric_limits<int>::max() - (int)1e8;
const ll INFLL = numeric_limits<ll>::max() - (ll)1e17;

int main() {
    int N;
    cin >> N;
    vector<vector<ll>> A(N + 1);
    for (int i = 0; i < N; i++) {
        ll X, C;
        cin >> X >> C;
        A[C].push_back(X);
    }
    for (int i = 1; i <= N; i++) {
        sort(A[i].begin(), A[i].end());
    }
    // 0: under, 1: upper
    vector<vector<pair<ll, int>>> dp(N + 1, vector<pair<ll, int>>(2, {INFLL, -1}));
    dp[0] = vector<pair<ll, int>>(2, {0, 0});
    for (int i = 1; i <= N; i++) {
        if (A[i].empty()) {
            dp[i] = dp[i - 1];
            continue;
        }
        dp[i][0].second = *A[i].begin();
        dp[i][1].second = *A[i].rbegin();
        ll dif = dp[i][1].second - dp[i][0].second;
        for (int a = 0; a <= 1; a++) {
            for (int b = 0; b <= 1; b++) {
                ll tmp = dif + abs(dp[i - 1][a].second - dp[i][b^1].second);
                dp[i][b].first = min(dp[i][b].first, dp[i - 1][a].first + tmp);
            }
        }
    }
    cout << min(dp[N][0].first + abs(dp[N][0].second), dp[N][1].first + abs(dp[N][1].second)) << endl;
}