#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) begin(v), end(v)
#define sz(v) v.size()
#define INF 1e18+9
#define EPSILON 1e-14
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main()
{
    ll N;
    cin >> N;
    vector<ll> X(N), C(N);
    vector<vector<ll>> ball(N+2, vector<ll>(0));
    ball.at(0).emplace_back(0);
    rep(i, N){
        cin >> X.at(i) >> C.at(i);
        ball.at(C.at(i)).emplace_back(X.at(i));
    }
    ball.at(N+1).emplace_back(0);
    rep(i, N+2){
        sort(all(ball.at(i)));
    }
    vector<vector<ll>> dp(N+2, vector<ll>(2, 0));
    //dp.at(i).at(0)は色iまでを回収して左端にいるときの最小時間
    //dp.at(i).at(1)は右端にいる
    ll p = 0;
    rep(i, N+1){
        if(ball.at(i+1).empty()){
            dp.at(i+1).at(0) = dp.at(i).at(0);
            dp.at(i+1).at(1) = dp.at(i).at(1);
        }
        else{
            ll preleft = ball.at(p).at(0);
            ll preright = ball.at(p).at(sz(ball.at(p))-1);
            ll left = ball.at(i+1).at(0);
            ll right = ball.at(i+1).at(sz(ball.at(i+1))-1);
            // preleft -> left
            dp.at(i+1).at(0) = abs(preleft-right) + right - left + dp.at(i).at(0);
            // preleft -> right
            dp.at(i+1).at(1) = abs(preleft-left) + right - left + dp.at(i).at(0);
            // preright -> left
            chmin(dp.at(i+1).at(0), abs(preright-right) + right - left + dp.at(i).at(1));
            // preright -> right
            chmin(dp.at(i+1).at(1), abs(preright-left) + right - left + dp.at(i).at(1));
            p = i+1;
        }
    }
    cout << min(dp.at(N+1).at(0), dp.at(N+1).at(1)) << '\n';
}
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>

using namespace std;

long long int max(long long int a,long long int b){
	if(a>b){
		return a;
	}
	return b;
}
long long int min(long long int a,long long int b){
	if(a<b){
		return a;
	}
	return b;
}

int main()
{
	int N;
	cin>>N;
	int colour[N][2];//stores min and max for each colour
	for(int i=0;i<N;i++){
		colour[i][0]=0;
		colour[i][1]=0;
	}
	for(int i=0;i<N;i++){
		int x,c;
		cin>>x>>c;
		if(colour[c-1][0]==0){
			colour[c-1][0]=x;
			colour[c-1][1]=x;
		}
		if(colour[c-1][0]>x){colour[c-1][0]=x;}
		if(colour[c-1][1]<x){colour[c-1][1]=x;}
	}
	long long int last_left[2];
	last_left[0]=0;
	last_left[1]=0;
	long long int last_right[2];//moves taken, then place of current person
	last_right[0]=0;
	last_right[1]=0;
	for(int i=0;i<N;i++){
		long long int next_left[2],next_right[2];//moves then place
		if(colour[i][0]==0){continue;}
		next_right[1]=colour[i][1];
		next_left[1]=colour[i][0];
		long long int distR=max(last_right[1]-next_right[1],next_right[1]-last_right[1]);
		long long int distL=max(last_left[1]-next_right[1],next_right[1]-last_left[1]);
		next_left[0]=min(last_right[0]+distR+next_right[1]-next_left[1],last_left[0]+distL+next_right[1]-next_left[1]);
		distR=max(last_right[1]-next_left[1],next_left[1]-last_right[1]);//changing to next color
		distL=max(last_left[1]-next_left[1],next_left[1]-last_left[1]);
		next_right[0]=min(last_right[0]+distR+next_right[1]-next_left[1],last_left[0]+distL+next_right[1]-next_left[1]);
		last_left[0]=next_left[0];
		last_left[1]=next_left[1];
		last_right[0]=next_right[0];
		last_right[1]=next_right[1];
	}
	cout<<min(last_right[0]+max(last_right[1],-last_right[1]),last_left[0]+max(last_left[1],-last_left[1]))<<endl;
}