//IQ134高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007
#define MODB  998244353 

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
    for (T& x: vec) { is >> x; }
    return is;
}

set<int> col;
vector<int> cs;
map<int, ll> mi, ma;

int main() {
	ll ans = 0;
	ll tmp = 0;
	int n; cin >> n;
	ll x; int c;
	REP(i, n){
		cin >> x >> c;
		if(col.find(c) == col.end()){
			col.insert(c);
			cs.push_back(c);
			mi[c] = x;
			ma[c] = x;
		}
		else{
			mi[c] = min(x, mi[c]);
			ma[c] = max(x, ma[c]);
		}
	}
	col.insert(0);
	cs.push_back(0);
	mi[0] = 0;
	ma[0] = 0;
	
	col.insert(200001);
	cs.push_back(200001);
	mi[200001] = 0;
	ma[200001] = 0;
	ll dp[2][2] = {};
	sort(cs.begin(), cs.end());
	
	REP(i, cs.size() - 1){
		dp[1][0] = min( dp[0][0] + abs(mi[cs[i]]-ma[cs[i+1]]) + abs(ma[cs[i+1]]-mi[cs[i+1]]), 
						dp[0][1] + abs(ma[cs[i]]-ma[cs[i+1]]) + abs(ma[cs[i+1]]-mi[cs[i+1]]) );
		dp[1][1] = min( dp[0][0] + abs(mi[cs[i]]-mi[cs[i+1]]) + abs(ma[cs[i+1]]-mi[cs[i+1]]), 
						dp[0][1] + abs(ma[cs[i]]-mi[cs[i+1]]) + abs(ma[cs[i+1]]-mi[cs[i+1]]) );
		swap(dp[0][0], dp[1][0]);
		swap(dp[0][1], dp[1][1]);	
	}
	cout << min(dp[0][0], dp[0][1]) << endl;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }

const int maxn = 1e6 + 7;
const ll inf = 1e18;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n; cin >> n;

    map<ll,set<ll>> mp;
    for (int i = 0; i < n; i++) {
        ll x,c;
        cin >> x >> c;
        mp[c].insert(x);
    }

    vector<ll> dp = {0,0};
    vector<ll> at = {0,0};

    mp[inf].insert(0);

    for (auto p: mp) {
        ll lval = *p.second.begin();
        ll rval = *p.second.rbegin();

        vector<ll> ndp = {inf,inf};
        vector<ll> nat = {lval,rval};

        ll totval = rval-lval;

        for (int i = 0; i < 2; i++) { // cur
            for (int j = 0; j < 2; j++) { // prev
                ndp[i] = min(ndp[i], dp[j]+totval+abs(nat[i^1]-at[j]));
            }
        }

        dp = ndp;
        at = nat;
    }

    ll ans = min(dp[0],dp[1]);
    cout << ans << "\n";
    
    return 0;
}
