#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>

#define N 200000
unsigned long long dp[N][2];

using namespace std;
int main(void) {
    unsigned long long n;
    cin >> n;
    map<long long, std::vector<long long> > balls;
    for (long long i = 0; i < n; i++) {
        long long x, c;
        cin >> x >> c;
        if (balls.count(c) == 0) {
            balls[c] = std::vector<long long>();
        }
        balls[c].push_back(x);
    }
    for (const auto& b: balls) {
        sort(balls[b.first].begin(), balls[b.first].end());
    }
    long long current_pos_left = 0;
    long long current_pos_right = 0;
    auto it = balls.begin();
    long long i;
    for (it = balls.begin(), i = 0; it != balls.end(); it++, i++) {
        long long left = (*it).second.front();
        long long right = (*it).second.back();
        if (it == balls.begin()) {
            dp[i][0] = abs(right) + right - left;
            dp[i][1] = abs(left) + right - left;
            current_pos_left = left;
            current_pos_right = right;
            //cout << "i = " << i << ", left = " << dp[i][0] << ", right = " << dp[i][1]
            //     << ", pos_left = " << current_pos_left << ", pos_right = " << current_pos_right << endl;
        } else {
            dp[i][0] = min(dp[i-1][0] + abs(current_pos_left - right) + right - left,
                           dp[i-1][1] + abs(current_pos_right - right) + right - left);
            dp[i][1] = min(dp[i-1][0] + abs(current_pos_left - left) + right - left,
                           dp[i-1][1] + abs(current_pos_right - left) + right - left);
            current_pos_left = left;
            current_pos_right = right;
            //cout << "i = " << i << ", left = " << dp[i][0] << ", right = " << dp[i][1]
            //     << ", pos_left = " << current_pos_left << ", pos_right = " << current_pos_right << endl;
        }
    }
    cout << min(dp[balls.size() - 1][0] + abs(current_pos_left), dp[balls.size() - 1][1] + abs(current_pos_right)) << endl;
}
#include<bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")


#define pb push_back
#define fi first
#define se second
#define ll long long
#define tp top()
#define fr front()
#define vi vector<int>
#define sz size()
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define mem(a,  b) memset(a,  (b),  sizeof(a))
#define clr(a) memset(a,  0,  sizeof(a))
#define sqr(x) ( (x) * (x) )
#define all(v) v.begin(),  v.end()

typedef pair<int, int> pii;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;



void base(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

#define N 200020

ll mins[N], maks[N];

ll memo[N][5];
int n; 
ll dp(int id, int state, ll pos){
	if (id == n+1) return abs(pos);
	if (memo[id][state] != -1) return memo[id][state];
	if (maks[id] == -1e18) return memo[id][state] = dp(id+1,state,pos);
	
	ll ans = 1e18;
	
	ans = min(ans, dp(id+1, 0, mins[id]) + abs(pos-maks[id]) + abs(maks[id]-mins[id]));
	ans = min(ans, dp(id+1, 1, maks[id]) + abs(pos-mins[id]) + abs(maks[id]-mins[id]));		
	return memo[id][state] = ans;
}
ll pos[N], c[N];

void solve(){
	mem(memo,-1);
	cin>>n;
	rep(i,1,n+1) {
		mins[i] = 1e18;
		maks[i] = -1e18;
	}
	rep(i,0,n){
		cin>>pos[i]>>c[i];
		maks[c[i]] = max(maks[c[i]], pos[i]);
		mins[c[i]] = min(mins[c[i]], pos[i]);
	}
	cout<<dp(1,0,0)<<"\n";
}

int main()
{
	base();
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}




