#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)

const ll big = 1000000001;
int main(){
  ll n; cin >> n;
  vector<ll> m1(n, big), m2(n, -big);
  rep(i, n){
    ll x, c;
    cin >> x >> c;
    c--;
    m1[c] = min(m1[c], x);
    m2[c] = max(m2[c], x);
  }
  P now1 = P(0, 0); // now/sum
  P now2 = P(0, 0); // now/sum
  ll next1, next2;
  rep(i, n){
    if (m1[i] == big) continue;
    next1 = now1.second + abs(now1.first-m2[i]) + abs(m1[i]-m2[i]);
    next1 = min(next1, now2.second + abs(now2.first-m2[i]) + abs(m1[i]-m2[i]));
    next2 = now1.second + abs(now1.first-m1[i]) + abs(m1[i]-m2[i]);
    next2 = min(next2, now2.second + abs(now2.first-m1[i]) + abs(m1[i]-m2[i]));
    now1 = P(m1[i], next1);
    now2 = P(m2[i], next2);
  }
  ll ans = min(abs(now1.first)+now1.second, abs(now2.first)+now2.second);
  cout << ans << endl;
  return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip> 
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>>ve;
int N,l,r,L,R;
long long LL,RR,tr,tl;
string getss(){//用getchar()读入string
	string k="";
	char a;
	a=getchar();
	while(a==-1||a==32||a==10)a=getchar();
	while(a!=-1&&a!=32&&a!=10){
		k+=a;
		a=getchar();
	}
	return k;
}
int main(){
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		ve.push_back(make_pair(b,a));
	}
	sort(ve.begin(),ve.end());
	pair<int,int>cur;
	cur=*ve.begin();
	LL=RR=l=r=L=R=0;
	L=cur.second;
	R=cur.second;
	for(pair<int,int>g:ve){
		if(g.first==cur.first){
			L=min(L,g.second);
			R=max(R,g.second);
		} else{
			cur=g;
			
			tr=R-L+min(LL+abs(L-l),RR+abs(L-r));
			tl=R-L+min(LL+abs(R-l),RR+abs(R-r));
			RR=tr;
			LL=tl;
			//cout<<RR<<" "<<LL<<endl;
			l=L;
			r=R;
			L=R=cur.second;
			//cout<<l<<" "<<r<<" "<<L<<" "<<R<<endl;
		}
	}
	tr=R-L+min(LL+abs(L-l),RR+abs(L-r));
	tl=R-L+min(LL+abs(R-l),RR+abs(R-r));
	RR=tr;
	LL=tl;
	//cout<<RR<<" "<<LL<<endl;
	//cout<<l<<" "<<r<<" "<<L<<" "<<R<<endl;
	printf("%lld",min(LL+abs(L),RR+abs(R)));
	return 0;
}