#include <bits/stdc++.h>
#define F(i, l, r) for(int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for(int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
using namespace std;
bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;}

inline long long read() {
 long long x = 0, fh = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar() ) if (ch == '-') fh = -1;
    for (; isdigit(ch); ch = getchar() ) x = (x<<1) + (x<<3) + (ch ^ '0');
    return x * fh;
}
struct node{
       long long pos,val;
	   bool operator<(const node &rhs)const{
           if(val==rhs.val)return pos<rhs.pos;  
		   return val<rhs.val;
	   }
}a[200005];
int n;
long long num[200005],cnt;
long long  dis[200005][2],tot[200005];
long long dp[200005][2];
const int inf=2147483640;
int main () {
#ifndef ONLINE_JUDGE
	File("e");
#endif
    n=read();
	F(i,1,n)a[i].pos=read(),a[i].val=read(),num[a[i].val]++;
    int lst=1,nxt=1;
	sort(a+1,a+n+1);
	F(i,1,n)dis[i][0]=inf,dis[i][1]=-inf;
//	F(i,1,n)cout<<a[i].pos<<" "<<a[i].val<<endl;
    F(i,1,n){
		dis[a[i].val][0]=min(dis[a[i].val][0],a[i].pos);
		dis[a[i].val][1]=max(dis[a[i].val][1],a[i].pos);
	}
//		cout<<dp[1][0]<<" "<<dp[1][1]<<endl;
//cout<<cnt<<endl;
    int lstl=0,lstr=0,nowl=0,nowr=0;
	num[n+1]=1;
	F(i,1,n+1){
		if(num[i]==0){
           dp[i][0]=dp[i-1][0];
		   dp[i][1]=dp[i-1][1];
		   continue;
		}
		nowl=dis[i][0];
		nowr=dis[i][1];
        dp[i][0]=min(dp[i-1][0]+abs(nowr-lstl),dp[i-1][1]+abs(nowr-lstr))+abs(nowr-nowl);
        dp[i][1]=min(dp[i-1][0]+abs(nowl-lstl),dp[i-1][1]+abs(nowl-lstr))+abs(nowr-nowl);
        lstl=nowl;
		lstr=nowr;
		//		cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<tot[i]<<endl;
	}
	printf("%lld\n",min(dp[n+1][1],dp[n+1][0]));
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(void){
    int N;
    vector<int> ColorMin;
    vector<int> ColorMax;
    cin >> N;
    ColorMin.assign(N, 0);
    ColorMax.assign(N, 0);
    for(int i=0; i<N; i++){
        int Xbuf, Cbuf;
        cin >> Xbuf >> Cbuf;
        Cbuf--;
        if(ColorMin[Cbuf]==0){
            ColorMin[Cbuf]=Xbuf;
        }else ColorMin[Cbuf]=min(ColorMin[Cbuf], Xbuf);
        if(ColorMax[Cbuf]==0){
            ColorMax[Cbuf]=Xbuf;
        }else ColorMax[Cbuf]=max(ColorMax[Cbuf], Xbuf);
    }
    long long dpl = 0;
    long long dpr = 0;
    int curl = 0;
    int curr = 0;
    for(int i=0; i<N; i++){
        if(ColorMin[i] == 0)continue;
        long long dplbuf = min(dpl+abs(curl-ColorMax[i]), dpr+abs(curr-ColorMax[i]))+ColorMax[i]-ColorMin[i];
        long long dprbuf = min(dpl+abs(curl-ColorMin[i]), dpr+abs(curr-ColorMin[i]))+ColorMax[i]-ColorMin[i];
        dpl = dplbuf;
        dpr = dprbuf;
        curl = ColorMin[i];
        curr = ColorMax[i];
    }
    cout << min(dpl+abs(curl), dpr+abs(curr)) << endl;
    return 0;
}