#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(ll i = 0; i < (ll)(n); ++i)
#define FOR(i, a, b) for(ll i=(a); i < (ll)(b); ++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;}

int main(){
	int n;
	cin >> n;
	vector<pair<ll,ll>> a(n);
	REP(i,n) cin >> a[i].first >> a[i].second;
	vector<vector<ll>> place(n+1);
	REP(i,n){
		place[a[i].second].push_back(a[i].first);
	}
	vector<pair<ll,ll>> ans(n+1);
	
	REP(i,n+1){
		if(place[i].size() <= 1) continue;
		sort(place[i].begin(), place[i].end());
	}
	
	pair<ll,ll> prevp = {0,0};
	ans[0] = {0,0};
	FOR(i,1,n+1){
		if(place[i].size() == 0){
			ans[i] = ans[i-1];
			continue;
		}
		
		ll length = abs(place[i].front()-place[i].back());
		ans[i].first = ans[i-1].first + abs(place[i].back()-prevp.first) + length;
		chmin(ans[i].first,  ans[i-1].second + abs(place[i].back()-prevp.second) + length);
		
		ans[i].second = ans[i-1].first + abs(place[i].front()-prevp.first) + length;
		chmin(ans[i].second,  ans[i-1].second + abs(place[i].front()-prevp.second) + length);
		
		prevp.first = place[i].front();
		prevp.second = place[i].back();
	}
	
	ll res = ans[n].first + abs(prevp.first);
	chmin(res, ans[n].second+abs(prevp.second));
	
	cout << res << endl;
	return 0;
}
#include<bits/stdc++.h>
#define MAXN 200000+10

using namespace std;

int n;
int l[MAXN],r[MAXN];
long long DP[MAXN][2];


int main()
{
	memset(DP,0x3f,sizeof(DP));
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		l[i] = r[i] = -1;
	
	for(int i=1;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		if(l[y] == -1) l[y] = r[y] = x;
		else l[y] = min(l[y],x),r[y] = max(r[y],x);
	}
	int s = 0;
	long long Ans;
	DP[0][0] = DP[0][1] = 0;
	n++;
	for(int t = 1;t <= n;t++)
	{
		if(l[t] == -1) continue;
				
		if(l[s] < l[t])
		{
			DP[t][0] = min(DP[t][0],DP[s][0] + r[t] - l[s] + r[t] - l[t]);
			DP[t][1] = min(DP[t][1],DP[s][0] + r[t] - l[s]);
		}
		else if(l[s] > r[t])
		{
			DP[t][0] = min(DP[t][0],DP[s][0] + l[s] - l[t]);
			DP[t][1] = min(DP[t][1],DP[s][0] + l[s] - l[t] + r[t] - l[t]);
		}
		else
		{
			DP[t][0] = min(DP[t][0],DP[s][0] + r[t] - l[t] + (r[t] - l[s]));
			DP[t][1] = min(DP[t][1],DP[s][0] + r[t] - l[t] + (l[s] - l[t]));
		}
		
		if(r[s] < l[t])
		{
			DP[t][0] = min(DP[t][0],DP[s][1] + r[t] - r[s] + r[t] - l[t]);
			DP[t][1] = min(DP[t][1],DP[s][1] + r[t] - r[s]);
		}
		else if(r[s] > r[t])
		{
			DP[t][0] = min(DP[t][0],DP[s][1] + r[s] - l[t]);
			DP[t][1] = min(DP[t][1],DP[s][1] + r[s] - l[t] + r[t] - l[t]);
		}
		else
		{
			DP[t][0] = min(DP[t][0],DP[s][1] + r[t] - l[t] + (r[t] - r[s]));
			DP[t][1] = min(DP[t][1],DP[s][1] + r[t] - l[t] + (r[s] - l[t]));
		}
		Ans = min(DP[t][0],DP[t][1]);
		s = t;
	}
	cout << Ans;
}