#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
  int N;
  cin >> N;
  std::unordered_map<int, int> mx;
  std::unordered_map<int, int> mn;

  rep(i, N)
  {
    int x, c;
    cin >> x >> c;

    if (mx.find(c) != mx.end())
    {
      mx[c] = std::max(mx[c], x);
      mn[c] = std::min(mn[c], x);
    }
    else
    {
      mx[c] = x;
      mn[c] = x;
    }
  }

  long long time1 = 0;
  long long time2 = 0;
  int pos1 = 0;
  int pos2 = 0;
  for (int i = 1; i <= N; i++)
  {
    if (mx.find(i) != mx.end())
    {
      long long diff = std::abs(mx[i] - mn[i]);
      long long tmp1 = time1;
      long long tmp2 = time2;
      // from pos1, mx -> mn
      tmp1 += std::abs(pos1 - mx[i]) + diff;

      // from pos2, mx -> mn
      tmp2 += std::abs(pos2 - mx[i]) + diff;

      long long tmp3 = time1;
      long long tmp4 = time2;
      // from pos1, mn -> mx
      tmp3 += std::abs(pos1 - mn[i]) + diff;

      // from pos2, mn -> mx
      tmp4 += std::abs(pos2 - mn[i]) + diff;

      time1 = std::min(tmp1, tmp2);
      time2 = std::min(tmp3, tmp4);

      pos1 = mn[i];
      pos2 = mx[i];
    }
  }

  time1 += std::abs(pos1);
  time2 += std::abs(pos2);

  cout << std::min(time1, time2) << endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    
    ll n; cin >> n;
    ll x[n], c[n]; zep(i, 0, n)cin >> x[i] >> c[i];
    
    map<ll, P> mp;
    zep(i, 0, n){
        if(mp.find(c[i]) == mp.end()){
            mp[c[i]] = P(x[i], x[i]);
        }
        mp[c[i]] = P(min(mp[c[i]].first, x[i]), max(mp[c[i]].second, x[i]));
    }

    ll m = mp.size();
    ll dp[m + 1][2]; memset(dp, 0, sizeof(dp));
    ll l = 0, r = 0;
    ll at = 0;
    for(auto it = mp.begin(); it != mp.end(); it++){
        ll L = (*it).second.first, R = (*it).second.second;
        
        dp[at + 1][0] = INF;
        dp[at + 1][1] = INF;

        if(R < l){
            dp[at + 1][0] = min(dp[at + 1][0], l - L + dp[at][0]);
        }else{
            dp[at + 1][0] = min(dp[at + 1][0], 2 * R - l - L + dp[at][0]);
        }

        if(R < r){
            dp[at + 1][0] = min(dp[at + 1][0], r - L + dp[at][1]);
        }else{
            dp[at + 1][0] = min(dp[at + 1][0], 2 * R - r - L + dp[at][1]);
        }
        
        if(L < l){
            dp[at + 1][1] = min(dp[at + 1][1], R + l - 2 * L + dp[at][0]);
        }else{
            dp[at + 1][1] = min(dp[at + 1][1], R - l + dp[at][0]);
        }

        if(L < r){
            dp[at + 1][1] = min(dp[at + 1][1], R + r - 2 * L + dp[at][1]);
        }else{
            dp[at + 1][1] = min(dp[at + 1][1], R - r + dp[at][1]);
        }
        
        l = L, r = R;
        at++;
        //cout << dp[at][0] << " " << dp[at][1] << endl;
    }
    print(min(dp[m][0] + abs(l), dp[m][1] + abs(r)))

    return 0;
}