#include <memory.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
ll n, minv[200111], maxv[200111];
ll reachMinv[200111], reachMaxv[200111];
vector<ll> ids;
int main(void) {
	scanf("%lld", &n);
	for (ll i = 0; i <= n + 1; i++) {
		minv[i] = 9e18;
		maxv[i] = -9e18;
	}
	ll lastV = -1;
	ids.push_back(0);
	for (ll i = 1; i <= n; i++) {
		ll a, b; scanf("%lld %lld", &a, &b);

		ids.push_back(b);

		minv[b] = min(minv[b], a);
		maxv[b] = max(maxv[b], a);
	}
	ids.push_back(n + 1);

	sort(ids.begin(), ids.end());
	ids.erase(unique(ids.begin(), ids.end()), ids.end());

	minv[0] = maxv[0] = minv[ids.back()] = maxv[ids.back()] = 0;
	reachMinv[0] = reachMaxv[0] = 0;
	for (ll i = 1; i < ids.size(); i++) {
		ll curi = ids[i];
		ll prei = ids[i - 1];

		ll minToMax = reachMinv[prei] + (maxv[prei] - minv[prei]);
		ll maxToMin = reachMaxv[prei] + (maxv[prei] - minv[prei]);

		reachMinv[curi] = min(
			minToMax + abs(maxv[prei] - minv[curi]),
			maxToMin + abs(minv[prei] - minv[curi])
		);
		reachMaxv[curi] = min(
			minToMax + abs(maxv[prei] - maxv[curi]),
			maxToMin + abs(minv[prei] - maxv[curi])
		);
	}

	printf("%lld\n", reachMinv[ids.back()]);
}
#include <bits/stdc++.h> 
using namespace std; 

#define ffor(i, a, b) for (int i = a ; i <= (int) b ; i++) 
#define rfor(i, a, b) for (int i = (int) a ; i >= b ; i--)  

#define vec vector 
#define PB push_back 

#define MP make_pair 
#define MT make_tuple 
#define F first 
#define S second 

using ll = long long int; // watch out for overflows in your code
using pii = pair<int,int>;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void debug() { cout << "\n"; };
template <typename T, typename... Targs> 
void debug(T t, Targs... args) {
    cout << t << " ";
    debug(args...);
}

template <typename T1, typename T2> 
inline ostream& operator << (ostream& os, const pair<T1,T2>& p) {
    return os << "(" << p.F << ", " << p.S << ")";
}

template <typename T> 
inline ostream& operator << (ostream& os, const vec<T>& v) {
    for(auto& t : v) {
        os << t << " ";
    }
    return os;
}

template <typename T> 
inline ostream& operator << (ostream& os, const vec<vec<T>>& m) {
    for(auto& v : m) {
        for(auto& t : v) {
            os << t << " ";
        }
        os << "\n";
    }
    return os;
}

template <typename T> 
inline ostream& operator << (ostream& os, const set<T>& v) {
    for(auto& t : v) {
        os << t << " ";
    }
    return os;
}

template <typename K, typename V> 
inline ostream& operator << (ostream& os, const map<K,V>& m) {
    for(auto& p : m) {
        os << p.F << " -> " << p.S << "\n";
    }
    return os;
}

const ll INF = 9 * 1e18;

int N;
vec<ll> lo, hi;

ll dist(ll st, ll fn, ll x) {
    return abs(x-st) + abs(st-fn);
}

vec<vec<ll>> dp;

void compute_dp() {
    dp.assign(N+1, vec<ll>(2,0));
    ffor(c, 1, N) {
        if(lo[c] == lo[c-1] && hi[c] == hi[c-1]) {
            dp[c][0] = dp[c-1][0];
            dp[c][1] = dp[c-1][1];
        }
        else {
            dp[c][0] = min(dp[c-1][0] + dist(hi[c], lo[c], lo[c-1]), dp[c-1][1] + dist(hi[c], lo[c], hi[c-1]));
            dp[c][1] = min(dp[c-1][0] + dist(lo[c], hi[c], lo[c-1]), dp[c-1][1] + dist(lo[c], hi[c], hi[c-1]));
        }
    }
}

////////////////////////////////////////////////////////////////////////

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

    cin >> N;
    lo.assign(N+1, INF);
    hi.assign(N+1, -INF);
    lo[0] = hi[0] = 0;
    ffor(i, 1, N) {
        ll x; int c;
        cin >> x >> c;
        lo[c] = min(lo[c], x);
        hi[c] = max(hi[c], x);
    }

    ffor(c, 1, N) {
        if(lo[c] == INF) lo[c] = lo[c-1];
        if(hi[c] == -INF) hi[c] = hi[c-1];
    }

    compute_dp();
    cout << min(dp[N][0] + abs(lo[N]), dp[N][1] + abs(hi[N])) << "\n";
}

