#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <sstream>
#include <cstring>
#include <bitset>
#include <stack>
using namespace std;
#define MP make_pair
#define PB push_back
const int INF=1e9;

int n,pos;
pair<int,int> p[100005];
bool del[100005];
priority_queue <pair<int,int> >q1,q2;
long long ans;

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		p[i]=MP(x,y);
		q1.push(MP(x,i));
		q2.push(MP(-y,i));
	}
	pos=0;
	for (int i=1;i<=n;i++)
	{
		int p1=-1,p2=-1,d1=-1,d2=-1;
		while (!q1.empty() && del[q1.top().second]) q1.pop();
		while (!q2.empty() && del[q2.top().second]) q2.pop();
		if (!q1.empty()) p1=q1.top().second;
		if (!q2.empty()) p2=q2.top().second;
		if (p1!=-1) d1=max(0,p[p1].first-pos);
		if (p2!=-1) d2=max(0,pos-p[p2].second);
		if (d1<=0 && d2<=0) break;
		if (d1<d2)
		{
			ans+=(long long)d2;
			del[p2]=true;
			pos=p[p2].second;
	//		cout<<p2<<endl;
		}
		else
		{
			ans+=(long long)d1;
			del[p1]=true;
			pos=p[p1].first;
	//		cout<<p1<<endl;
		}
	}
	ans+=(long long)(abs(pos));
	cout<<ans;
	return 0;
}