/*
ID: gurban1
LANG: C++
TASK: 
*/
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int maxn=2e5+5;
int n;
ll dp[maxn][2];
pair<ll,ll>p[maxn];
ll l[maxn],r[maxn];

int main(){
	// freopen("","r",stdin);
	// freopen("","w",stdout);

	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n;
	for(int i = 1;i <= n;i++) cin >> p[i].second>>p[i].first;

	sort(p+1,p+n+1);
	
	for(int i = n;i >= 1;i--){
		if(p[i].first != p[i+1].first) r[p[i].first] = p[i].second;
		if(p[i].first != p[i-1].first) l[p[i].first] = p[i].second;
	}

	int last = -1;
	for(int i = n;i >= 1;i--){
		if(l[i] == 0) continue;
		if(last == -1){
			dp[i][0] = llabs(l[i]-r[i]) + llabs(r[i]);
			dp[i][1] = llabs(l[i]-r[i]) + llabs(l[i]);
		}
		else {
			dp[i][0] = llabs(l[i]-r[i]) + min(llabs(r[i]-l[last])+dp[last][0],llabs(r[i]-r[last]) + dp[last][1]);
			dp[i][1] = llabs(l[i]-r[i]) + min(llabs(l[i]-l[last])+dp[last][0],llabs(l[i]-r[last]) + dp[last][1]);
		}
		last = i;
	}
	cout<<min(dp[last][0]+llabs(l[last]),dp[last][1]+llabs(r[last]));
}
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ggdem=b;i<ggdem;++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;

const int MAXN=400005;
vector<int> g[MAXN]; // [0,n)->[0,m)
int n,m;
int mt[MAXN],mt2[MAXN],ds[MAXN];
bool bfs(){
	queue<int> q;
	memset(ds,-1,sizeof(ds));
	fore(i,0,n)if(mt2[i]<0)ds[i]=0,q.push(i);
	bool r=false;
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int y:g[x]){
			if(mt[y]>=0&&ds[mt[y]]<0)ds[mt[y]]=ds[x]+1,q.push(mt[y]);
			else if(mt[y]<0)r=true;
		}
	}
	return r;
}
bool dfs(int x){
	for(int y:g[x])if(mt[y]<0||ds[mt[y]]==ds[x]+1&&dfs(mt[y])){
		mt[y]=x;mt2[x]=y;
		return true;
	}
	ds[x]=1<<30;
	return false;
}
int mm(){
	int r=0;
	memset(mt,-1,sizeof(mt));memset(mt2,-1,sizeof(mt2));
	while(bfs()){
		fore(i,0,n)if(mt2[i]<0)r+=dfs(i);
	}
	return r;
}

ll a[MAXN],b[MAXN];

int main(){FIN;
	cin>>n;
	fore(i,0,n)cin>>a[i]>>b[i];
	fore(i,0,n){
		g[i].pb(a[i]);
		g[i].pb(b[i]);
	}
	cout<<mm()<<"\n";
	
	return 0;
}