#include "bits/stdc++.h"
#include <iomanip>

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define int ll
typedef pair<int,int> pii;
#define fi first
#define se second
#define Sort(a) sort(a.begin(),a.end())
#define rep(i,n) for(int i = 0;i < (n) ; i++) 
#define REP(i,n) for(int i = 0;i < (n) ; i++) 
#define MP(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define INF LLONG_MAX/4
#define INTINF INT_MAX/2
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
template<class T> inline bool chmax(T &a, T b){if(a < b){a = b;return true;}return false;}
template<class T> inline bool chmin(T &a, T b){if(a > b){a = b;return true;}return false;}
/*----------------------------------------------------------------*/
// const int MOD = 998244353;
const int MOD = 1000000007;
/*----------------------------------------------------------------*/
double p = 3.14159265359;
/*----------------------------------------------------------------*/
signed main(){

	int n;
	cin >> n;
	map<int,pii> mp;
	rep(i,n){
		int x,c;
		cin >> x >> c;
		auto itr = mp.find(c);
		if(itr != mp.end()){
			mp[c].first = min(mp[c].first,x);
			mp[c].second = max(mp[c].second,x);
		}else{
			mp[c] = {x,x};
		}
	}

	int ans_lr = 0;
	int ans_rl = 0;
	int pl = 0;
	int pr = 0;

	for(auto p:mp){
		int l = p.second.first;
		int r = p.second.second;
		int next_ans_lr = min(ans_lr + abs(pr-l),ans_rl + abs(pl-l)) + abs(l-r);
		int next_ans_rl = min(ans_lr + abs(pr-r),ans_rl + abs(pl-r)) + abs(l-r);
		ans_lr = next_ans_lr;
		ans_rl = next_ans_rl;
		pl = l;
		pr = r;
	}

	cout << min(ans_lr + abs(pr),ans_rl + abs(pl)) << endl;
	return 0;
}
/*----------------------------------------------------------------*/

// g++ -std=gnu++17 code1.cpp
// sudo pip3 install --upgrade online-judge-tools
// rm -r -f test;oj dl https://atcoder.jp/contests/agc048/tasks/agc048_a
// rm -r -f test;oj d https://atcoder.jp/contests/abc197/tasks/abc197_a

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;


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

	cin >> N;
	vector<int>l(N + 1, INT_MAX);
	vector<int>r(N + 1, INT_MIN);
	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		l[b] = min(a, l[b]);
		r[b] = max(a, r[b]);
	}
	long long int lp = 0, rp = 0, lsum = 0, rsum = 0;
	for (int i = 1; i <= N; i++) {
		if (l[i] == INT_MAX)continue;
		long long int nlsum = LLONG_MAX, nrsum = LLONG_MAX;
		nlsum = min(lsum + abs(lp - r[i]), rsum + abs(rp - r[i])) + abs(r[i] - l[i]);
		nrsum = min(lsum + abs(lp - l[i]), rsum + abs(rp - l[i])) + abs(r[i] - l[i]);
		lp = l[i], rp = r[i];
		lsum = nlsum, rsum = nrsum;
	}
	cout << min(lsum+abs(lp), rsum+abs(rp)) << endl;
}