#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<ll,ll>;
ll MAX = 0;
const ll INF = 1001001001001001001;

ll n;
vector<ll> ma(200005,-INF);
vector<ll> mi(200005,INF);

int main(){
    cin >> n;
    rep(i,n){
        ll x,c;
        cin >> x >> c;
        ma[c] = max(ma[c],x);
        mi[c] = min(mi[c],x);
    }
    ma[0] = 0;
    mi[0] = 0;

    ll dp[200005][2];
    dp[0][0] = 0;
    dp[0][1] = 0;

    rep(i,n){
        if(ma[i+1] == -INF){
            dp[i+1][0] = dp[i][0];
            dp[i+1][1] = dp[i][1];
            ma[i+1] = ma[i];
            mi[i+1] = mi[i];
        }
        else if(mi[i+1] == ma[i+1]){
            dp[i+1][0] = min(dp[i][1] + abs(mi[i+1] - mi[i]),dp[i][0] + abs(mi[i+1] - ma[i]));
            dp[i+1][1] = dp[i+1][0];
        }
        else{
            dp[i+1][0] = min(dp[i][1] + abs(mi[i+1] - mi[i]) + abs(ma[i+1]-mi[i+1]),dp[i][0] + abs(mi[i+1] - ma[i]) + abs(ma[i+1]-mi[i+1]));
            dp[i+1][1] = min(dp[i][1] + abs(ma[i+1] - mi[i]) + abs(ma[i+1]-mi[i+1]),dp[i][0] + abs(ma[i+1] - ma[i]) + abs(ma[i+1]-mi[i+1]));
        }
    }

    cout << min(dp[n][0]+abs(ma[n]),dp[n][1]+abs(mi[n])) << endl;
/*
    rep(i,n+1){
        cout << dp[i][0] << ' ' << dp[i][1] << endl;

    }
*/
}
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) ((x).begin()), ((x).end())

#define READ(x) (cin >> (x))
#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))
#define WRITE_YESNO(x) (WRITE_N((x) ? "Yes" : "No"))

#define PRECISE_COUT std::cout << std::setprecision(15) << std::fixed

bool xor_logic(bool x, bool y) { return (x && y) || (!x && !y); }

struct state_t {
  ll time;
  ll place;
};

#define INF 1000000001

ll solve(vector<ll> X, vector<int> C) {
  size_t N = X.size();
  // balls[C] = {X1, X2, ...}
  map<int, vector<ll>> balls;

  for (size_t i = 0; i < N; i++) {
    if (balls.count(C[i]) == 0) {
      balls[C[i]] = {};
    }
    balls[C[i]].push_back(X[i]);
  }
  balls[INF].push_back(0);

  vector<state_t> states{{0, 0}};
  for (auto &&p : balls) {
    vector<ll> ball_places = p.second;
    sort(ALL(ball_places));

    if (ball_places.size() == 1) {
      if (states.size() == 1) {
        state_t s = {states[0].time + abs(states[0].place - ball_places[0]),
                     ball_places[0]};
        states[0] = s;
      } else {
        vector<state_t> sv;
        for (auto &&s : states) {
          sv.push_back(
              {s.time + abs(s.place - ball_places[0]), ball_places[0]});
        }

        states = sv;
      }
    } else {
      vector<state_t> sv;

      ll left = ball_places[0];
      ll right = ball_places.back();

      for (auto &&s : states) {
        if (s.place <= left) {
          sv.push_back({s.time + abs(s.place - right), right});
        } else if (s.place >= right) {
          sv.push_back({s.time + abs(s.place - left), left});
        } else {
          sv.push_back({s.time + abs(s.place - right) + (right - left), left});
          sv.push_back({s.time + abs(s.place - left) + (right - left), right});
        }
      }
      states = sv;
    }

    // trim
    sort(ALL(states),
         [](state_t &s1, state_t &s2) { return s1.time < s2.time; });
    size_t n = (states.size() + 9) / 10;
    set<size_t> delete_i_s;
    for (size_t i = 0; i < n; i++) {
      for (size_t j = n; j < states.size(); j++) {
        if (states[j].time - states[i].time >
            abs(states[j].place - states[i].place)) {
          delete_i_s.insert(j);
        }
      }
    }
    vector<size_t> delete_i_v(ALL(delete_i_s));
    for (int i = delete_i_v.size() - 1; i >= 0; i--) {
      states.erase(states.begin() + delete_i_v[i]);
    }
  }

  sort(ALL(states), [](state_t &s1, state_t &s2) { return s1.time < s2.time; });
  return states[0].time;
}

int main() {
  // get values from input
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<ll> X(N);
  vector<int> C(N);

  for (size_t i = 0; i < N; i++) {
    cin >> X[i] >> C[i];
  }

  // main procedure

  // output
  cout << solve(X, C) << endl;

  return 0;
}