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

int main(){
  int n; cin >> n;
  vector<vector<P> > v(n + 1);
  vector<P> v2(n + 1, P(1e18, -1e18));
  rep(i, n){
    ll x, c; cin >> x >> c;
    v2[c].first = min(v2[c].first, x);
    v2[c].second = max(v2[c].second, x);
  }
  rep(i, n){
    if(v2[i + 1].first == 1e18) continue;
    v[i + 1].push_back(P(v2[i + 1].first, 1e18));
    v[i + 1].push_back(P(v2[i + 1].second, 1e18));
  }
  v[0].push_back(P(0, 0));
  for(int i = 0; i < n; i++){
    // cout << i << endl;
    if(v[i + 1].size() == 0){
      v[i + 1] = v[i];
      continue;
    }
    for(auto& now: v[i]){
      ll nowx = now.first, nowv = now.second;
      ll l = v[i + 1][0].first, r = v[i + 1][v[i + 1].size() - 1].first;
      for(auto& next: v[i + 1]){
        ll nextx = next.first;
        ll nextv = nowv;
        if(nowx < l){
          nextv += r - nowx + r - nextx;
        }
        else if(nowx > r){
          nextv += nowx - l + nextx - l;
        }
        else{
          nextv += r - l;
          if(nextx < nowx){
            nextv += abs(r - nowx) + abs(l - nextx);
          }
          else{
            nextv += abs(l - nowx) + abs(r - nextx);
          }
        }
        next.second = min(next.second, nextv);
      }
    }
    // cout << "------" << i+1 << "-------" << endl;
    // for(auto& e: v[i+1]){
    //   cout << e.first << " , " << e.second << endl;
    // }
  }
  ll ans = 1e18;
  for(auto& e: v[n]){
    ans = min(ans, e.second + abs(e.first));
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
  
#define ordered_set tree<pair <int, int>, null_type,less<pair <int, int>>, rb_tree_tag,tree_order_statistics_node_update> 
 
const int MAXN = 3e5 + 20, MAXM = 16e4 + 5, MOD = 1e9 + 7, SQ = 450;
const long long INF = 1e9 + 10;
int l[MAXN], r[MAXN];
long long dp[MAXN][2];

void solve() {
	int n;
	cin >> n;
	
	for (int i = 1; i <= n; i++) {
		l[i] = INF;
		r[i] = -INF;
	}
	
	vector <int> id;
	l[0] = 0;
	r[0] = 0;
	l[n + 1] = 0;
	r[n + 1] = 0;
	id.push_back(0);
	id.push_back(n + 1);
	for (int i = 0; i < n; i++) {
		int x, y;
		cin >> x >> y;
		
		l[y] = min(x, l[y]);
		r[y] = max(x, r[y]);
		
		id.push_back(y);
	}	
	
	sort(id.begin(), id.end());
	id.resize(unique(id.begin(), id.end()) - id.begin());
	
	int t = id.size();
	dp[0][0] = 0;
	dp[0][1] = 0;
	for (int i = 1; i < id.size(); i++) {
		dp[i][0] = dp[i - 1][1] + abs(r[id[i - 1]] - r[id[i]]) + abs(r[id[i]] - l[id[i]]);
		dp[i][0] = min(dp[i - 1][0] + abs(l[id[i - 1]] - r[id[i]]) + abs(r[id[i]] - l[id[i]]), dp[i][0]);
		dp[i][1] = dp[i - 1][1] + abs(r[id[i - 1]] - l[id[i]]) + abs(r[id[i]] - l[id[i]]);
		dp[i][1] = min(dp[i - 1][0] + abs(l[id[i - 1]] - l[id[i]]) + abs(r[id[i]] - l[id[i]]), dp[i][1]);
	}
	
	cout << min(dp[t - 1][0], dp[t - 1][1]) << endl;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int q = 1;
//	cin >> q;
	while (q--) {
		solve();
	}	
}
