#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define sz(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define rep(x) for (int ii = 0; ii < x; ii++)

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  map<int, vector<int>> M;
  rep(n) {
    int x, id;
    cin >> x >> id;
    M[id].push_back(x);
  }

  for (const auto& [k, v] : M) sort(all(M[k]));

  ll ans = 0;
  vector<int> keys;
  for (const auto& [k, v] : M) {
    keys.push_back(k);
    for (int i = 1; i < sz(v); i++) {
      ans += v[i] - v[i - 1];
    }
  }

  int m = sz(M);

  vector<vector<ll>> dp(m, vector<ll>(2, 0));
  dp[0][0] = abs(M[keys[0]][0]);
  dp[0][1] = abs(M[keys[0]].back());
  for (int i = 1; i < m; i++) {
    int k = keys[i];
    dp[i][0] = min(
        dp[i - 1][0] + abs(M[k][0] - M[keys[i - 1]].back()),
        dp[i - 1][1] + abs(M[k][0] - M[keys[i - 1]][0]));
    dp[i][1] =
        min(dp[i - 1][0] +
                abs(M[k].back() - M[keys[i - 1]].back()),
            dp[i - 1][1] +
                abs(M[k].back() - M[keys[i - 1]][0]));
  }
  ans += min(dp[m - 1][0] + abs(M[keys.back()].back()),
             dp[m - 1][1] + abs(M[keys.back()][0]));

  cout << ans;
}
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <sstream>
#include <ostream>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <iomanip>
#include <set>
#include <bitset>
//#include<unordered_map>
#define INT_MINs -2000000000
#define INT_MAXs 1000000001
#define MID int mid=(l+r)/2
#define REP1(i,x,y) for(int i=x;i<y;i++)
#define REP2(i,x,y) for(int i=x;i<=y;i++)
#define ls (2*k)
#define rs (2*k+1)
#define lson l,mid,2*k
#define rson mid+1,r,2*k+1
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define IOS ios::sync_with_stdio(0);cin.tie(NULL);
#define pb(a) push_back(a)
#define pi acos(-1)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const ll ba=131;
//ll qk(ll x,ll y){ll ans=1;while(y){if(y&1) ans=ans*x;y>>=1;x=x*x;}return ans;}
const int dx[8] = { 0,-1,0,1,-1,-1,1,1}, dy[8] = { -1,0,1,0,-1,1,-1,1};
const int dxx[8]= {2,1,-1,-2,-2,-1, 1, 2},dyy[8]= {1,2,2,1, -1,-2,-2,-1};
ll n;
ll a[200005],b[200005],l[200005],r[200005];
ll dp[200005][2];
void solve(){
     cin>>n;
     ll maxx=0,minn=INF;
     for(int i=1;i<=200000;i++){
        r[i]=-INF;
        l[i]=INF;
     }
     for(int i=1;i<=n;i++){
        cin>>a[i]>>b[i];
        l[b[i]]=min(l[b[i]],a[i]);
        r[b[i]]=max(r[b[i]],a[i]);
        //cout<<l[b[i]]<<' '<<r[b[i]]<<' '<<1111<<endl;
        minn=min(minn,b[i]);
        maxx=max(maxx,b[i]);
     }

     ll sum1=0,sum2=0;
     l[minn-1]=r[minn-1]=0;
     for(int i=minn;i<=maxx;i++){
        if(r[i]==-INF) { l[i]=l[i-1],r[i]=r[i-1],dp[i][0]=dp[i-1][0],dp[i][1]=dp[i-1][1];continue;}
        dp[i][0]=min(dp[i-1][0]+r[i]-l[i]+abs(l[i-1]-r[i]),dp[i-1][1]+r[i]-l[i]+abs(r[i-1]-r[i]));
        dp[i][1]=min(dp[i-1][0]+r[i]-l[i]+abs(l[i-1]-l[i]),dp[i-1][1]+r[i]-l[i]+abs(r[i-1]-l[i]));
     }
     cout<<min(dp[maxx][0]+abs(l[maxx]),dp[maxx][1]+abs(r[maxx]))<<endl;
}

int main(){
    IOS;
    solve();
    return 0;
}
