#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

/* Some Libraries */

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin>>N;
    vector<vll> v(N+1, vll(2,-1));
    v[N][0]=v[N][1]=0;
    rep(i,N){
        ll x,c; cin>>x>>c; c--;
        auto &vec = v[c];
        if (vec[0]!=-1){
            chmin(vec[0], x);
            chmax(vec[1], x);
        }else{
            vec[0]=vec[1]=x;
        }
    }
    int prev_i=-1;
    vll dp(2);
    rep(i,N+1){
        if (v[i][0]==-1) continue;
        vll nxt(2,LLONG_MAX);
        rep(j,2){
            int cur = (prev_i!=-1) ? v[prev_i][j] : 0;
            chmin(nxt[0], dp[j]+abs(cur-v[i][1])+(v[i][1]-v[i][0]));
            chmin(nxt[1], dp[j]+abs(cur-v[i][0])+(v[i][1]-v[i][0]));
        }
        swap(dp,nxt);
        prev_i = i;
    }
    cout<<dp[0]<<endl;
    return 0;
}
#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

typedef long long lli;
typedef pair<int, int> pii;

const int maxn = 2e5 + 5;
const lli inf = 1e18;

int n;
int x[maxn], c[maxn];
bool app[maxn];

int l[maxn], r[maxn];

int lst_l, lst_r;
lli f[2], nf[2];

void read_input()
{
    cin >> n;
    for(int i = 1; i <= n; ++i)
    {
        cin >> x[i] >> c[i];
        app[c[i]] = true;
        l[c[i]] = r[c[i]] = x[i];
    }
}

void solve()
{
    for(int i = 1; i <= n; ++i)
    {
        l[c[i]] = min(l[c[i]], x[i]);
        r[c[i]] = max(r[c[i]], x[i]);
    }
    lst_l = 0; lst_r = 0;
    f[0] = f[1] = 0;
    for(int i = 1; i <= n; ++i)
    {
        if(!app[i]) continue;
        nf[0] = min(abs(lst_l - r[i]) + f[0] + abs(l[i] - r[i]), abs(lst_r - r[i]) + f[1] + abs(l[i] - r[i]));
        nf[1] = min(abs(lst_l - l[i]) + f[0] + abs(l[i] - r[i]), abs(lst_r - l[i]) + f[1] + abs(l[i] - r[i]));
        lst_l = l[i]; lst_r = r[i];
        f[0] = nf[0];
        f[1] = nf[1];
    }
    cout << min(f[0] + abs(lst_l), f[1] + abs(lst_r));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    read_input();
    solve();
}

