#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define mp make_pair
#define pb push_back
#define all(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = n; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n); (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int INF = 1080000000;
constexpr long long LINF = 4611686015206162431;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N;
    cin >> N;
    vc<int> X(N), C(N);
    rep (i, N) cin >> X[i] >> C[i];

    vc<int> cmin(N, INF), cmax(N, -INF);
    rep (i, N) {
        chmin(cmin[C[i]-1], X[i]);
        chmax(cmax[C[i]-1], X[i]);
    }

    int l = 0, r = 0;
    ll lt = 0, rt = 0;
    rep (i, N) {
        if (cmin[i] == INF) continue;
        ll tlt = min(abs(l - cmax[i]) + lt, abs(r - cmax[i]) + rt) + cmax[i] - cmin[i];
        ll trt = min(abs(l - cmin[i]) + lt, abs(r - cmin[i]) + rt) + cmax[i] - cmin[i];
        lt = tlt, rt = trt;
        l = cmin[i];
        r = cmax[i];
    }

    cout << min(lt + abs(l), rt + abs(r)) << endl;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[2][2];
int main(){
	int n;
	scanf("%d",&n);
	ll a,b;
	map<int,vector<ll> > mapa;
	for(int i = 0; i < n; i++){
		scanf("%lld %lld",&a,&b);
		mapa[b].push_back(a);
	}
	a = b = 0;
	ll izq,der; izq = 0; der = 0;
	int op = 1;
	for(auto x : mapa){
		sort(x.second.begin(), x.second.end());
		int y = x.second.size();
		dp[op][0] = min(dp[1-op][0]+abs(x.second[0]-a),dp[1-op][1] + abs(x.second[0]-b)) + abs(x.second[y-1]-x.second[0]);
		dp[op][1] = min(dp[1-op][0]+abs(x.second[y-1]-a),dp[1-op][1] + abs(x.second[y-1]-b)) + abs(x.second[y-1]-x.second[0]);

		a = x.second[y-1];
		b = x.second[0];
		op = 1-op;
	}
	printf("%lld\n", min(dp[1-op][0] + abs(a), dp[1-op][1] + abs(b)));
	return 0;
}