//teja349
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip> 
//setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >

vector< multiset<ll> > adj(1234567),revadj(1234567);
multiset<ll> seti,seti1;
multiset<ll>::iterator it;
ll l[123456],r[123456];
ll bit[2][523456];
ll update(ll typ,ll pos,ll val){
	while(pos<512345){
		bit[typ][pos]+=val;
		pos+=pos&(-pos);
	}
	return 0;
}
ll query(ll typ,ll pos){
	ll ans=0;
	while(pos>0){
		ans+=bit[typ][pos];
		pos-=pos&(-pos);
	}
	return ans;
}
int main(){
    std::ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    ll i;
    ll mid=100005;

    rep(i,n){
    	cin>>l[i]>>r[i];
    	l[i]+=100005;
    	r[i]+=100005;
    	update(0,r[i],1);
    	update(1,l[i],1);
    	seti1.insert(-1*l[i]);
    	seti.insert(r[i]);
    	adj[l[i]].insert(-1*r[i]);
    	revadj[r[i]].insert(l[i]);

    }

    //return 0;
    //ll mid=0;
    ll pos=mid;
    ll cnt=n;
    ll ans=0;
    ll left=n;
    ll val1,val2,haha,wow;
    while(cnt--){
    	ll val1=query(1,pos);
    	val1=left-val1;
    	val2=query(0,pos-1);
    	//continue;
    	if(val1==0 && val2==0){
    		break;
    	}
    	if(val1==val2){
    		if(pos<mid){
    			val1++;
    		}
    		else{
    			val2++;
    		}
    	}
    	left--;
    	//continue;
    	if(val1>val2){
    		it=seti1.begin();
    		haha=*it;
    		haha*=-1;
    		assert(haha>pos);
    		ans+=abs(haha-pos);
    		update(1,haha,-1);
    		pos=haha;
    		seti1.erase(it);
    		it=adj[haha].begin();
    		wow=*it;
    		wow*=-1;
    		update(0,wow,-1);
    		seti.erase(seti.find(wow));
    		swap(haha,wow);
    		revadj[haha].erase(revadj[haha].find(wow));
    		adj[wow].erase(adj[wow].find(-1*haha));
    	}
    	else{
    		it=seti.begin();
    		haha=*it;
    		//haha*=-1;
    		assert(haha<pos);
    		ans+=abs(haha-pos);
    		update(0,haha,-1);
    		pos=haha;
    		seti.erase(it);
    		it=revadj[haha].begin();
    		wow=*it;
    		update(1,wow,-1);
    		seti1.erase(seti1.find(-1*wow));
    		//swap(haha,wow);
    		revadj[haha].erase(revadj[haha].find(wow));
    		adj[wow].erase(adj[wow].find(-1*haha));
    	} 
    }
    ans+=abs(pos-mid);
    cout<<ans<<endl;

    
}

Measured memory_bytes: 144000