#include <bits/stdc++.h> 
#define ll long long
#define F first
#define S second
#define FF first.first
#define FS first.second
#define pb push_back
using namespace std;
const ll N=1000006, INF=1e18, MOD=1e9+7;
     
ll n, m, t, a[N][2], f[N], q=1, e, o, u, x, y, k, ans, Ans0[2], Ans1[2], mx;
vector <ll> v;
string s[101];

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

//	cin>>q;
	while(q--){
		cin>>n;
		for (int i=1; i<=n; i++)a[i][0]=INF,a[i][1]=-INF;
		
		for (int i=1; i<=n; i++){
			cin>>x>>y;
			a[y][0]=min(x, a[y][0]);
			a[y][1]=max(x, a[y][1]);
		}
		
		x=0;
		for (int i=1; i<=n+1; i++){
			if(a[i][0]<INF){
				Ans1[0]=min(Ans0[0]+abs(a[x][0]-a[i][1]), Ans0[1]+abs(a[x][1]-a[i][1]))+a[i][1]-a[i][0];
				Ans1[1]=min(Ans0[0]+abs(a[x][0]-a[i][0]), Ans0[1]+abs(a[x][1]-a[i][0]))+a[i][1]-a[i][0];
				Ans0[0]=Ans1[0];
				Ans0[1]=Ans1[1];
				x=i;
			}
//			cout<<Ans0[0]<<" "<<Ans0[1]<<"____\n";
		}
		cout<<min(Ans0[0], Ans0[1]);
	}
}


// Always remember that you are absolutely unique, just like everyone else!

#include <iostream>
#include <vector>
using namespace std;


// DEBUGGING SECTION
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}


#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)


#define MOD     1000000007
#define INF     (ll)1e18
#define ll      long long int
#define pll     pair<ll, ll>
#define vll     vector<ll>
#define vvll	vector<vll>
#define vpll    vector<pll>
#define pb      push_back
#define endl    '\n' // Remove if interactive
#define ff      first
#define ss      second
#define all(x)  x.begin(), x.end()
#define FOR(i, a, b)   for(int i = a; i < b; i++)


ll power(ll x, ll y) { ll ans = 1; x %= MOD; while (y) {if (y & 1)ans = (x * ans) % MOD; x = (x * x) % MOD; y >>= 1;} return ans;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};


//#include <algorithm>
//#include <cstring>
//#include <map>
//#include <set>
//#include <queue>

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

// comparator(A, B) -> should return true if A needs to come before B!
// ALWAYS RETURN FALSE IF A == B

vector<pll> vec;
vvll arr;
vvll dp, dp2;

ll helper(ll in, int end);

ll helper2(ll in, int end) {
	if (in == arr.size() - 1)
		return 0;
	if (dp2[in][end] != -1)
		return dp2[in][end];

	ll one = helper(in + 1, 0);
	ll two = helper(in + 1, 1);

	ll ans = min(abs(arr[in][end] - arr[in + 1][0]) + one, abs(arr[in][end] - arr[in + 1][1]) + two);;

	dp2[in][end] = ans;
	return ans;
}

ll helper(ll in, int end) {
	if (in == arr.size() - 1) {
		return 0;
	}
	if (dp[in][end] != -1)
		return dp[in][end];
	ll one = helper2(in, 1 - end);
	ll two = helper2(in, 1 - end);
	ll dist = abs(arr[in][1] - arr[in][0]);
	ll ans = dist + min(one, two);
	dp[in][end] = ans;
	return ans;
}

void solve() {
	int n;
	cin >> n;
	vec.resize(n + 10, {INF, -INF});
	dp.resize(n + 10, vll(2, -1));
	dp2.resize(n + 10, vll(2, -1));

	ll x, c;
	FOR(i, 0, n) {
		cin >> x >> c;
		--c;
		vec[c].ff = min(vec[c].ff, x);
		vec[c].ss = max(vec[c].ss, x);
	}

	FOR(i, 0, n) {
		if (vec[i].ff != INF) {
			arr.pb({vec[i].ff, vec[i].ss});
		}
	}
	arr.pb({0, 0});
	//debug(arr);
	cout << min(abs(arr[0][0]) + helper(0, 0), abs(arr[0][1]) + helper(0, 1));
	//debug(dp);
}



int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	// freopen("error.txt", "w", stderr);
	freopen("output.txt", "w", stdout);
#endif

	fastio;
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
}
