#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }
long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    vector<ll> x(n), c(n);
    rep(i, n) cin >> x[i] >> c[i];
    map<ll, pair<ll, ll>> mc;
    rep(i, n) {
        if (mc.count(c[i]) == 0) {
            mc[c[i]] = make_pair(x[i], x[i]);
        }
        else {
            chmin(mc[c[i]].first, x[i]);
            chmax(mc[c[i]].second, x[i]);
        }
    }
    mc[n + 1] = make_pair(0, 0);
    map<ll, ll> dp;
    dp[0] = 0;
    for(auto &x : mc) {
        map<ll, ll> ndp;
        ll minv = x.second.first, maxv = x.second.second;
        for(auto &y : dp) {
            // max -> min
            {
                ll val = y.second + abs(y.first - maxv) + abs(maxv - minv);
                if (ndp.count(minv) == 0) {
                    ndp[minv] = val;
                }
                else {
                    chmin(ndp[minv], val);
                }
            }
            // min -> max
            {
                ll val = y.second + abs(y.first - minv) + abs(minv - maxv);
                if (ndp.count(maxv) == 0) {
                    ndp[maxv] = val;
                }
                else {
                    chmin(ndp[maxv], val);
                }
            }
        }
        dp = ndp;
    }
    ll ans = LONG_LONG_MAX;
    for(auto &x : dp) chmin(ans, x.second);
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
const int N=2e5+5;
ll mn[N],mx[N];
int main()
{
    int n;
    cin>>n;
    for (int i=1;i<=n;++i)
        mn[i]=2e9,mx[i]=-2e9;
    for (int i=0;i<n;++i){
        ll x,c;
        cin >> x >> c;
        mn[c]=min(mn[c],x);
        mx[c]=max(mx[c],x);
    }
    ll dp[n + 1][2];
    dp[0][0]=dp[0][1]=0;
    bool first = true;
    ll res = 0;
    ll lmx = -1;
    ll lmn = -1;
    for (int i = 1; i <= n; ++i)
    {
        if (mn[i] == 2e9){
            dp[i][0] = dp[i - 1][0];
            dp[i][1] = dp[i - 1][1];
        }
        else
        {
            if (first)//第一次
            {
                dp[i][0]=mx[i]-mn[i]+abs(mx[i]);
                dp[i][1]=mx[i]-mn[i]+abs(mn[i]);
                first = false;
                lmx = mx[i];
                lmn = mn[i];
            }
            else
            {
                dp[i][0] = min(abs(lmx - mx[i]) + dp[i - 1][1], abs(lmn - mx[i]) + dp[i - 1][0]) + mx[i] - mn[i];
                dp[i][1] = min(abs(lmx - mn[i]) + dp[i - 1][1], abs(lmn - mn[i]) + dp[i - 1][0]) + mx[i] - mn[i];
                lmx = mx[i];
                lmn = mn[i];
            }
            res = min(dp[i][0] + abs(mn[i]), dp[i][1] + abs(mx[i]));
        }
    }
    cout << res << endl;
    system("pause");
    return 0;
}