#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
#define int long long

template<typename T>
void chmin(T &a,T b){
  if(a>b)a=b;
}
template<typename T>
void chmax(T &a,T b){
  if(a<b)a=b;
}
const int INF=1e9+1;

signed main(){
  int n;cin>>n;
  vector<int> l(n,INF),r(n,-INF);
  REP(i,n){
    int x,c;cin>>x>>c;c--;
    chmin(l[c],x);
    chmax(r[c],x);
  }
  int tmpl=0,tmpr=0,nowl=0,nowr=0;
  REP(i,n){
    if(l[i]>r[i])continue;
    int kaka;
    kaka=min(abs(nowl-r[i])+r[i]-l[i]+tmpl,abs(nowr-r[i])+r[i]-l[i]+tmpr);
    tmpr=min(abs(nowl-l[i])+r[i]-l[i]+tmpl,abs(nowr-l[i])+r[i]-l[i]+tmpr);
    tmpl=kaka;
    nowl=l[i],nowr=r[i];
  }
  cout<<min(tmpl+abs(nowl),tmpr+abs(nowr))<<endl;
}

#include <bits/stdc++.h>

using namespace std;

const int N=2e5+10;

long long dis(long long x,long long y){
    return abs(x-y);
}

int main()
{

    int n;
    scanf("%d",&n);
    vector<int> lft(n+1,1e9),rit(n+1,-1e9),C;
    for(int i=0;i<n;++i){
        int x,c;
        scanf("%d%d",&x,&c);
        lft[c]=min(lft[c],x);
        rit[c]=max(rit[c],x);
        C.push_back(c);
    }
    lft[0]=rit[0]=0;

    sort(C.begin(),C.end());
    C.erase(unique(C.begin(),C.end()),C.end());
    C.push_back(0);

    vector<vector<long long> >dp(2,vector<long long>(n+1,0));

    dp[0][C[0]]=dis(0,rit[C[0]])+dis(rit[C[0]],lft[C[0]]);
    dp[1][C[0]]=dis(0,lft[C[0]])+dis(lft[C[0]],rit[C[0]]);

    for(int i=1;i<C.size();++i){
        dp[0][C[i]]=min(dp[0][C[i-1]]+dis(lft[C[i-1]],rit[C[i]])+dis(rit[C[i]],lft[C[i]])
                        ,dp[1][C[i-1]]+dis(rit[C[i-1]],rit[C[i]])+dis(rit[C[i]],lft[C[i]]));
        dp[1][C[i]]=min(dp[0][C[i-1]]+dis(lft[C[i-1]],lft[C[i]])+dis(rit[C[i]],lft[C[i]])
                        ,dp[1][C[i-1]]+dis(rit[C[i-1]],lft[C[i]])+dis(rit[C[i]],lft[C[i]]));
    }

    printf("%lld\n",min(dp[0][0],dp[1][0]));

    return 0;
}
