#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    int64_t x, c;
    cin >> n;
    vector<vector<int64_t>> balls(n + 1, vector<int64_t>(0));
    for (size_t i = 0; i < n; i++)
    {
        cin >> x >> c;
        balls[c].push_back(x);
    }
    for (int i = 1; i <= n; i++)
    {
        sort(balls[i].begin(), balls[i].end());
    }

    const int64_t INF = 1LL << 60;
    vector<vector<int64_t>> dp(n + 1, vector<int64_t>(2, INF));

    int64_t l = 0, r = 0, cost_l = 0, cost_r = 0;
    for (int64_t i = 1; i <= n; i++)
    {
        if (balls[i].empty())
            continue;
        auto l2 = balls[i][0];
        auto r2 = balls[i][balls[i].size() - 1];

        int64_t cost_l2 = INF, cost_r2 = INF;

        // l->r2->l2
        cost_l2 = min(cost_l2, cost_l + abs(r2 - l2) + abs(l - r2));
        // r->r2->l2
        cost_l2 = min(cost_l2, cost_r + abs(r - r2) + abs(r2 - l2));

        // l->l2->r2
        cost_r2 = min(cost_r2, cost_l + abs(l - l2) + abs(r2 - l2));
        // r->l2->r2
        cost_r2 = min(cost_r2, cost_r + abs(r - l2) + abs(r2 - l2));

        l = l2;
        r = r2;
        cost_l = cost_l2;
        cost_r = cost_r2;
    }

    int64_t ans = min(cost_l + abs(l), cost_r + abs(r));

    cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for(int i = 0; i < (n); i++)
#define ALL(a) (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define RSORT(a) sort((a).rbegin(),(a).rend())
#define ll long long
#define pb(a) push_back(a)

#define PRINTV(v) {for(ll deB=0; deB<(v).size(); deB++){cout << (v)[deB] << " ";}cout << endl;}

int main(){
	ll N;
	cin >> N;
	vector<ll> X(N), C(N);
	REP(i,N){
		cin >> X[i] >> C[i];
	}

	vector<ll> cnum(N, 0);
	vector<ll> cmin(N, INT64_MAX);
	vector<ll> cmax(N, INT64_MIN);
	REP(i,N){
		cnum[C[i]-1] ++;
		if(X[i] < cmin[C[i]-1]) cmin[C[i]-1] = X[i];
		if(X[i] > cmax[C[i]-1]) cmax[C[i]-1] = X[i];
	}

	ll hi_dis = 0;
	ll hi_pos = 0;
	ll low_dis = 0;
	ll low_pos = 0;
	REP(i, N){
		if(cnum[i] == 0) continue;
		ll hi2low = hi_dis + ((hi_pos > cmax[i])?(hi_pos - cmax[i]):(cmax[i] - hi_pos)) + (cmax[i] - cmin[i]);
		ll low2low = low_dis + ((low_pos > cmax[i])?(low_pos - cmax[i]):(cmax[i] - low_pos)) + (cmax[i] - cmin[i]);
		ll hi2hi = hi_dis + ((hi_pos > cmin[i])?(hi_pos - cmin[i]):(cmin[i] - hi_pos)) + (cmax[i] - cmin[i]);
		ll low2hi = low_dis + ((low_pos > cmin[i])?(low_pos - cmin[i]):(cmin[i] - low_pos)) + (cmax[i] - cmin[i]);

		hi_dis = (hi2hi < low2hi) ? hi2hi : low2hi;
		low_dis = (hi2low < low2low) ? hi2low : low2low;

		hi_pos = cmax[i];
		low_pos = cmin[i];
	}
	hi_dis += ((hi_pos > 0)?hi_pos:-hi_pos);
	low_dis += ((low_pos > 0)?low_pos:-low_pos);

	ll ans = (hi_dis < low_dis) ? hi_dis : low_dis;

	cout << ans << endl;
	
	return 0;
}
