#include<ctime>
#include<cstdio>
#include<cctype>
#include<algorithm>
#define ll long long
using namespace std;
const ll N=2e5+7;
ll read() {
	char c;
	ll x=0,f=1;
	while(!isdigit(c=getchar()))
		f-=2*(c=='-');
	while (isdigit(c)){
		x=x*10+(c-48)*f;
		c=getchar();
	}
	return x;
}
ll n,x,c,las,l[N],r[N],f[N][2];
int main() {
	#ifndef ONLINE_JUDGE
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
	#endif
	clock_t t1=clock();
	//--------
	n=read();
	for(ll i=1;i<=n;++i){
		l[i]=1e18;
		r[i]=-1e18;
	}
	for(ll i=1;i<=n;++i){
		x=read();
		c=read();
		l[c]=min(l[c],x);
		r[c]=max(r[c],x);
	}
	++n;
	l[n]=r[n]=0;
	for(ll i=1;i<=n;++i){
		if(l[i]>r[i])
			continue;
		f[i][0]=1e18;
		f[i][1]=1e18;
		if(l[las]<=l[i])
			f[i][1]=min(f[i][1],f[las][0]+r[i]-l[las]);
		else
			f[i][1]=min(f[i][1],f[las][0]+l[las]-l[i]*2+r[i]);
		if(r[las]<=l[i])
			f[i][1]=min(f[i][1],f[las][1]+r[i]-r[las]);
		else
			f[i][1]=min(f[i][1],f[las][1]+r[las]-l[i]*2+r[i]);
		if(l[las]>=r[i])
			f[i][0]=min(f[i][0],f[las][0]+l[las]-l[i]);
		else
			f[i][0]=min(f[i][0],f[las][0]+r[i]*2-l[las]-l[i]);
		if(r[las]>=r[i])
			f[i][0]=min(f[i][0],f[las][1]+r[las]-l[i]);
		else
			f[i][0]=min(f[i][0],f[las][1]+r[i]*2-r[las]-l[i]);
		las=i;
	}
	printf("%lld",min(f[n][0],f[n][1]));
	//--------
	clock_t t2=clock();
	fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC);
	return 0;
}
//ABC_197_E_
//
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define reps(i,s,n) for(int i=s;i<n;++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main()
{
    int n;
    cin >> n;
    vector<P> cx;
    vector<ll> L = {0},R = {0};
    rep(i,n)
    {
        int c,x;
        cin >> x >> c;
        cx.emplace_back(c,x);
    }
    sort(cx.begin(),cx.end());
    map<ll,bool> used;
    for(auto cxi:cx)
    {
        ll ci,xi;
        tie(ci,xi) = cxi;

        if(!used[ci])
        {
            L.emplace_back(xi);
            R.emplace_back(xi);
            used[ci] = true;
        } 
        else
        {
            int idx = L.size()-1;
            L[idx] = min(L[idx],xi);
            R[idx] = max(R[idx],xi);
        }
    }
    int s = L.size()-1;
    vector<vector<ll>> dp(s+5, vector<ll> (2));
    dp[0][0] = 0;

    rep(i,s)
    {
        dp[i+1][0] = min(dp[i][0] + abs(L[i] - R[i+1]),
                         dp[i][1] + abs(R[i] - R[i+1]))
                         + abs(R[i+1]-L[i+1]);
        dp[i+1][1] = min(dp[i][0] + abs(L[i] - L[i+1]),
                         dp[i][1] + abs(R[i] - L[i+1]))
                         + abs(R[i+1]-L[i+1]);
      
    }
    ll ans = min(dp[s][0]+abs(L[s]),dp[s][1]+abs(R[s]));
    cout << ans << endl;
    return 0;
}