#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y))
#define chmax(x,y) x = max((x),(y))
#define popcount(x) __builtin_popcount(x)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;

int main(){
    int n;
    cin >> n;
    vector<ll> x(n),c(n);
    rep(i,n){
        cin >> x[i] >> c[i];
    }
    vector<ll> dp(2,0);
    vector<vector<ll>> G(n+2);
    G[0].push_back(0);
    G[n+1].push_back(0);
    rep(i,n){
        G[c[i]].push_back(x[i]);
    }
    rep(i,n+1){
        sort(G[i].begin(),G[i].end());
        //rep(j,(ll)G[i].size()) cout << G[i][j] << " ";
        //cout << endl;
    }

    int before = 0;
    for(int i=1;i<=n+1;i++){
        if((int)G[i].size() == 0) continue;
        vector<ll> dp_new(2,LINF);
        ll l = G[before][0];
        ll r = G[before].back();
        if(l < G[i][0]){
            dp_new[0] = dp[0] + G[i].back() - l + G[i].back() - G[i][0];
            dp_new[1] = dp[0] + G[i].back() - l;
        }else if(l < G[i].back()){
            dp_new[0] = dp[0] + G[i].back() - l + G[i].back() - G[i][0];
            dp_new[1] = dp[0] + l - G[i][0] + G[i].back() - G[i][0];
        }else{
            dp_new[0] = dp[0] + l - G[i][0];
            dp_new[1] = dp[0] + l - G[i][0] + G[i].back() - G[i][0];
        }

        if(r < G[i][0]){
            chmin(dp_new[0],dp[1] + G[i].back() - r + G[i].back() - G[i][0]);
            chmin(dp_new[1],dp[1] + G[i].back() - r);
        }else if(r < G[i].back()){
            chmin(dp_new[0],dp[1] + G[i].back() - r + G[i].back() - G[i][0]);
            chmin(dp_new[1],dp[1] + r - G[i][0] + G[i].back() - G[i][0]);
        }else{
            chmin(dp_new[0],dp[1] + r - G[i][0]);
            chmin(dp_new[1],dp[1] + r - G[i][0] + G[i].back() - G[i][0]);
        }

        before = i;
        swap(dp,dp_new);
        //cout << dp[0] << " " << dp[1] << endl;
    }
    cout << min(dp[0],dp[1]) << endl;

    return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int MAX = 200010;
const int64_t INF = 1e18;
vector<vector<int>> G(MAX,vector<int>(0));
int64_t dp[MAX][2];

int main(){
  int N;
  cin >> N;
  for(int i=0; i<N; i++){
    int x, c;
    cin >> x >> c;
    G[c].push_back(x);
  }
  G[N+1].push_back(0);
  for(int i=0; i<MAX; i++){
    for(int j=0; j<2; j++){
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  dp[0][1] = 0;
  int64_t l = 0;
  int64_t r = 0;
  for(int i=1; i<=N+1; i++){
    if(G[i].size() == 0){
      dp[i][0] = dp[i-1][0];
      dp[i][1] = dp[i-1][1];
      continue;
    }
    sort(G[i].begin(),G[i].end());
    int g = G[i].size();
    //大きい方に進んで小さい方に進む
    dp[i][0] = min(dp[i-1][0] + abs(l-G[i][g-1]) + abs(G[i][g-1] - G[i][0]),
                  dp[i-1][1] + abs(r-G[i][g-1]) + abs(G[i][g-1] - G[i][0]));
    //小さい方に進んで大きい方に進む
    dp[i][1] = min(dp[i-1][0] + abs(l-G[i][0]) + abs(G[i][g-1] - G[i][0]),
                  dp[i-1][1] + abs(r-G[i][0]) + abs(G[i][g-1] - G[i][0]));
    l = G[i][0];
    r = G[i][g-1];
  }
  cout << min(dp[N+1][0],dp[N+1][1]) << endl;
}