#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
using namespace std;
typedef long long ll;

vector<ll> X, C;
vector<array<ll, 2>> v;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n; X.resize(n); 
	C.resize(n); v.resize(n + 1, { (1 << 30), -(1 << 30) });
	for (int i = 0; i < n; i++) cin >> X[i] >> C[i];

	v[0][0] = v[0][1] = 0; for (int i = 0; i < n; i++) {
		v[C[i]][0] = min(v[C[i]][0], X[i]);
		v[C[i]][1] = max(v[C[i]][1], X[i]);
	}

	vector<vector<ll>> dp(n + 1, vector<ll>(2, 0));

	while (v[n][0] == (1 << 30)) { n--; }
	dp[n][0] = v[n][1] - v[n][0] + abs(v[n][1]); 
	dp[n][1] = (v[n][1] - v[n][0]) + abs(v[n][0]);
	for (int i = n - 1; i >= 0; i--) {
		if (v[i][0] == (1 << 30)) { 
			dp[i][0] = dp[i + 1][0]; dp[i][1] = dp[i + 1][1]; 
			v[i][0] = v[i + 1][0]; v[i][1] = v[i + 1][1];
			continue;
		}
		ll d00 = abs(v[i][0] - v[i + 1][0]), d01 = abs(v[i][0] - v[i + 1][1]);
		ll d10 = abs(v[i][1] - v[i + 1][0]), d11 = abs(v[i][1] - v[i + 1][1]);
		dp[i][0] = v[i][1] - v[i][0] + min(d10 + dp[i + 1][0], d11 + dp[i + 1][1]);
		dp[i][1] = v[i][1] - v[i][0] + min(d00 + dp[i + 1][0], d01 + dp[i + 1][1]);
	}

	cout << dp[0][0] << '\n';
	cin.ignore(2); return 0;
}
#include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <string>
#include <algorithm> 
#include <set>
#include <sstream>
#include <time.h>
#include <bitset>
#include <iomanip>
#include <queue>
#include <assert.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rew(i, n) for (int i = 1; i < (int)(n); i++)
using namespace std;
typedef long long int ll;
const double PI = 3.14159265358979323846;
using Graph = vector <vector<pair<ll, ll>>>;

int main() {
	ll maxval = 1e18;
	ll N; cin >> N;
	vector<bool>exist(N + 1, false);
	vector<ll>left(N + 1, maxval), right(N + 1, -maxval);
	rep(i, N) {
		ll X, C; cin >> X >> C;
		exist[C] = true;
		left[C] = min(left[C], X);
		right[C] = max(right[C],X);
	}
	ll lft = 0, rgt = 0;
	ll posl = 0, posr = 0;
	rep(i, N + 1) {
		if (!exist[i])continue;
		ll dist = right[i] - left[i];
		ll temp1 = min(lft + abs(posl - left[i]) + dist, rgt + abs(posr - left[i]) + dist);
		
		ll temp2 = min(rgt + abs(posr - right[i]) + dist,lft + abs(posl - right[i]) + dist);
		rgt = temp1;
		lft = temp2;
		posl = left[i];
		posr = right[i];
	}
	ll ans = min(lft + abs(posl), rgt + abs(posr));
	cout << ans << endl;
}