#include<bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
template<typename T> void dbg_a(T *a,int l,int r){cerr<<" {";for(int i = l;i<r;i++) cerr<<a[i]<<", ";cerr<<a[r]<<"}"<<endl;}
typedef long long ll;
#define int ll
inline int read(){
    int f = 1,r = 0;
    char c = getchar();
    while(c<'0'||c>'9'){if(c == '-') f = -1; c = getchar();}
    while(c>='0'&&c<='9'){ r = (r<<1) + (r<<3) + c - '0';c = getchar();}
    return f*r;
}
typedef pair<int,int> PII;
#define fi first
#define se second
#define mst(a,b) memset(a,b,sizeof(a))
#define For(i,a,b) for(int i = a;i<=b;i++)
#define For_(i,a,b) for(int i = a;i>=b;i--)
#define _for(i,a) for(int i = 0;i<a;i++)
#define All(x) x.begin(),x.end()
//    For(i,1,n) For(j,1,m) printf("f[%lld][%lld] = %lld\n",i,j,f[i][j]);
//const ll INF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10,M = N*2 + 10;
const int mod = 998244353;
int n;
vector<int> G[N];
int minl,minr,locl,locr;
void solve(){
    n = read();
    For(i,1,n){
        int x = read(),c = read();
        G[c].emplace_back(x);
    }
    For(i,1,n){
        if(!G[i].size()) continue;
        ll ml,mr,l,r;
        l = *min_element(All(G[i])),r = *max_element(All(G[i]));
        ml = minl + abs(locl - r) + r - l;
        ml = min(ml,minr + abs(locr - r) + r - l);
        mr = minr + abs(locr - l) + r - l;
        mr = min(mr,minl + abs(locl - l) + r - l);
        minl = ml,minr = mr,locl = l,locr = r;
    }
    cout<<min(abs(locl) + minl,abs(locr) + minr)<<"\n";
}

signed main(){
    int T = 1;
    //T = read();
    while(T--) solve();
    system("pause");
    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std;
#define inf 200005
#define INF 0x3f3f3f3f

int n;
int color[inf][2];
long long int dp[inf][2]; //dp[i][0] is the minimum steps after we get every i-color ball from the left, dp[i][1] is the minimum steps after we get every i-color ball from the right
int book[inf];
int main()
{
    int i,j;
    int x,c;
    scanf("%d",&n);
    for(i=1;i<=n;i++)       //init
    {
        color[i][0]=INF;    //the end position of color[i]
        color[i][1]=-INF;   //the begin position of color[i]
    }
    for(i=1;i<=n;i++)        //read data in disorder
    {
        scanf("%d%d",&x,&c);
        book[c]=1;
        if(x<color[c][0])color[c][0]=x; //update the begin position
        if(x>color[c][1])color[c][1]=x; //update the end position
    }

    //begin dp...
    dp[0][0]=dp[0][1]=0;
    for(i=1;i<=n;i++)
    {
        if(!book[i])
        {
            dp[i][0]=dp[i-1][0];
            dp[i][1]=dp[i-1][1];
            color[i][0]=color[i-1][0];
            color[i][1]=color[i-1][1];
            continue;
        }
        dp[i][0]=min(dp[i-1][0]+ abs(color[i-1][1]-color[i][0]), dp[i-1][1] + abs(color[i-1][0]-color[i][0]) );
        dp[i][0]+=color[i][1]-color[i][0];
        dp[i][1]=min(dp[i-1][0]+ abs(color[i-1][1]-color[i][1]), dp[i-1][1] + abs(color[i-1][0]-color[i][1]) );
        dp[i][1]+=color[i][1]-color[i][0];
    }
    printf("%lld\n",min(dp[n][0]+abs(color[n][1]),dp[n][1]+abs(color[n][0])));  //return to the origin
    return 0;
}
