#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for (int i = 0; i < (int)(n) ; i++)
#define rep2(i,a,b) for (int i = (a); i < (int)(b); i++)
#define repR(i,n) for (int i = (int)(n)-1; i >= 0; i--)
#define ENDL '\n'
#define pb push_back
#define SIZE(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define UNIQUE(a) a.erase(unique(all(a)),a.end());
#define fi first
#define se second
#define debug(x) cout<<(#x)<<" is : "<<x<<endl;
#define debugc(vec) {cout<<(#vec)<<" is below; ---"<<endl;for(auto c:vec)cout<<c<<" ";cout<<endl<<"----------------"<<endl;}
const int INF = 1'073'741'823;
const ll INFLL = 4'611'686'018'427'387'903LL;
const ld PI = 3.14159265358979323846L;

int main() {
    ll n; cin>>n;
    map<ll, pair<ll,ll>> mp;
    rep(i, n) {
        ll a,b;
        cin>>a>>b;
        if(mp.count(b)) {
            auto& [mn, mx] = mp[b];
            mn=min(mn, a);
            mx=max(mx, a);
        }
        else mp[b] = {a,a};
    }
    vector<pair<ll,ll>> vec;
    ll ans=0;
    for(auto [a,b]:mp) {
        vec.pb(b); //vec.size()==m
        ans += b.se-b.fi;
    }
    ll m=mp.size();
    vector<vector<ll>> dp(m,vector<ll>(2,0));

    rep(i, m) {
        if(i==0) {
            dp[i][0] = abs(vec[i].fi);
            dp[i][1] = abs(vec[i].se);
        }
        else {
            dp[i][0] = min(dp[i-1][0]+abs(vec[i].fi-vec[i-1].se),dp[i-1][1]+abs(vec[i].fi-vec[i-1].fi));
            dp[i][1] = min(dp[i-1][0]+abs(vec[i].se-vec[i-1].se),dp[i-1][1]+abs(vec[i].se-vec[i-1].fi));
        }
    }
    cout << ans + min(dp[m-1][0]+abs(vec[m-1].se), dp[m-1][1]+abs(vec[m-1].fi)) << endl;
}
#include <bits/stdc++.h>
using namespace std;

int main()
{
    size_t N;
    cin >> N;

    vector<long long> X(N);
    vector<size_t> C(N);
    vector<vector<long long>> ball(N + 1);
    for (size_t i = 0; i < N; i++)
    {
        cin >> X[i] >> C[i];
        ball[C[i]].push_back(X[i]);
    }

    // dp[i] := (the most left ans, the most left index)
    // ep[i] := (the most right ans, the most right index)
    vector<pair<long long, long long>> dp(N + 1);
    vector<pair<long long, long long>> ep(N + 1);
    dp[0] = make_pair(0ll, 0ll);
    ep[0] = make_pair(0ll, 0ll);
    for (size_t i = 1; i <= N; i++)
    {
        if (ball[i].empty())
        {
            dp[i] = dp[i - 1];
            ep[i] = ep[i - 1];
            continue;
        }
        sort(ball[i].begin(), ball[i].end());
        dp[i] = make_pair(
            min(
                dp[i - 1].first + abs(dp[i - 1].second - ball[i].back()) + abs(ball[i].back() - ball[i].front()),
                ep[i - 1].first + abs(ep[i - 1].second - ball[i].back()) + abs(ball[i].back() - ball[i].front())),
            ball[i].front());
        ep[i] = make_pair(
            min(
                dp[i - 1].first + abs(dp[i - 1].second - ball[i].front()) + abs(ball[i].front() - ball[i].back()),
                ep[i - 1].first + abs(ep[i - 1].second - ball[i].front()) + abs(ball[i].front() - ball[i].back())),
            ball[i].back());
    }
    cout << min(dp.back().first + abs(dp.back().second), ep.back().first + abs(ep.back().second)) << endl;
}
