#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200005;
int n;
int pos[N][2], ans[2];
int32_t main(int32_t argc, char const *argv[])
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        int x, c;
        cin >> x >> c;
        if (!pos[c][0])
            pos[c][0] = pos[c][1] = x;
        else
        {
            pos[c][0] = min(pos[c][0], x);
            pos[c][1] = max(pos[c][1], x);
        }
    }
    int pv = -1;
    for (int i = 1; i <= n; ++i)
    {
        if (!pos[i][0])
            continue;
        //cout << pos[i][0] << ' ' << pos[i][1] << '\n';
        if (pv == -1)
        {
            ans[0] += abs(pos[i][0]);
            ans[1] += abs(pos[i][1]);
            ans[0] += pos[i][1] - pos[i][0];
            ans[1] += pos[i][1] - pos[i][0];
            pv = i;
        }
        else
        {
            int x = ans[0];
            int y = ans[1];
            ans[0] = min(x + abs(pos[i][0] - pos[pv][1]), y + abs(pos[i][0] - pos[pv][0]));
            ans[1] = min(x + abs(pos[i][1] - pos[pv][1]), y + abs(pos[i][1] - pos[pv][0]));
            ans[0] += pos[i][1] - pos[i][0];
            ans[1] += pos[i][1] - pos[i][0];
            pv = i;
        }
    }
    cout << min(ans[0] + abs(pos[pv][1]), ans[1] + abs(pos[pv][0])) << '\n';
    return 0;
}

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set     tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
// find_by_order(k):    It returns to an iterator to the kth element (counting from zero) in the set
// order_of_key(k) :    It returns to the number of items that are strictly smaller than our item k
#define fast            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb              push_back
#define f               first
#define s               second
#define F(i,a,b)        for(i=a;i<b;i++)
#define nl              "\n"
#define sp              " " 
#define all(c)          (c).begin(),(c).end()
#define rev(i,b,a)      for(int i=b;i>=a;i--)
#define iota            cout<<-1<<nl
#define cty             cout<<"YES"<<nl
#define ctn             cout<<"NO"<<nl
#define lmax            LLONG_MAX
#define lmin            LLONG_MIN
#define sz(v)           (v).size()
#define deci(n)         fixed<<setprecision(n)
#define c(x)            cout<<(x)
#define csp(x)          cout<<(x)<<" "
#define c1(x)           cout<<(x)<<nl
#define c2(x,y)         cout<<(x)<<" "<<(y)<<nl
#define c3(x,y,z)       cout<<(x)<<" "<<(y)<<" "<<(z)<<nl
#define c4(a,b,c,d)     cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<nl
#define c5(a,b,c,d,e)   cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<nl
#define c6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<nl
typedef long double     ld;
typedef long long       ll;
typedef vector<ll>      vll;
typedef pair<ll,ll>     pll;
typedef vector<pll>     vpll;
const int mod=998244353;
const double pi=3.14159265358979323846264338327950288419716939937510582097494459230;
// priority_queue<t>

ll poww(ll a, ll b) { ll res=1; while(b) { if(b&1)	res=(res*a); a=(a*a); b>>=1; } return res; }
ll modI(ll a, ll m=mod) { ll m0=m,y=0,x=1;  if(m==1) return 0;  while(a>1)  { ll q=a/m; ll t=m;  m=a%m;  a=t; t=y; y=x-q*y;  x=t; } if(x<0) x+=m0; return x;}
ll powm(ll a, ll b,ll m=mod) {ll res=1;	while(b) {	if(b&1)	res=(res*a)%m;	a=(a*a)%m; b>>=1;	}	return res;}
//*******************************************************************************************************************************************//
void iamzeus()
{ ll i,j,x=0,y,ans=0,n,m,d,cnt=0,z,c=2,k;
  
 cin>>n;
 vll v[n+1];
 for(i=0;i<n;i++){
     cin>>x>>y;
  v[y].pb(x);
     
 }
 pll dp[n+1];
 dp[0].f=0,dp[0].s=0;
 x=0,y=0;
 for(i=1;i<=n;i++)
     sort(all(v[i]));
 for(i=1;i<=n;i++)
 {
     if(v[i].size()==0){
         dp[i].f=dp[i-1].f,dp[i].s=dp[i-1].s;
         continue;
     }
   ll  x1=v[i][0],y1=v[i].back();
     dp[i].f=min(dp[i-1].f+2*max((ll)0,y1-x)+x-x1,dp[i-1].s+2*max((ll)0,y1-y)+y-x1);
      dp[i].s=min(dp[i-1].f+2*max((ll)0,x-x1)+y1-x,dp[i-1].s+2*max((ll)0,y-x1)+y1-y);
     x=x1,y=y1;
// c2(dp[i].f,dp[i].s);
 }
 dp[n].f+=abs(x),dp[n].s+=abs(y);
 c1(min(dp[n].f,dp[n].s));
};
int main()
{fast;
    int t;
  t=1;
//cin >>t;
 while(t--)
     iamzeus();
}
