#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\n'
#define debug(x) std::cerr << #x << " = " << (x) << endl;

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll<<60;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }

template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }

ll N;
ll X[200010], C[200010];
std::vector<ll> vs[200010];
std::map<ll, ll> dp[2];

void chminMp( std::map<ll, ll> &mp, ll idx, ll x ) {
  if( !mp.count(idx) )
    mp[idx] = x;
  else
    mp[idx] = std::min( mp[idx], x );

  return;
}

int main() {
  std::cin >> N;

  rep( i, N ) {
    std::cin >> X[i] >> C[i];
    --C[i];

    vs[C[i]].emplace_back(X[i]);
  }

  rep( i, N )
    std::sort(all(vs[i]));

  ll x = 0;

  dp[0][0] = 0;

  ll p = 0;

  auto prv = dp[0], nxt = dp[1];

  rep( i, N ) {
    if( vs[i].empty() )
      continue;

    nxt.clear();

    itr( it, prv ) {
      ll x = it->first;

      ll idx = std::lower_bound(all(vs[i]), x)-vs[i].begin();

      if( idx && vs[i].size()-idx ) {
        ll d1 = llabs(vs[i][0]-x);
        ll d2 = llabs(vs[i][vs[i].size()-1]-x);

        chminMp(nxt, vs[i][vs[i].size()-1], prv[x]+d1*2+d2);
        chminMp(nxt, vs[i][0], prv[x]+d1+d2*2);
      } else if( idx ) {
        ll d1 = llabs(vs[i][0]-x);
        
        chminMp(nxt, vs[i][0], prv[x]+d1);
      } else if( vs[i].size()-idx ) {
        ll d2 = llabs(vs[i][vs[i].size()-1]-x);

        
        chminMp(nxt, vs[i][vs[i].size()-1], prv[x]+d2);
      }
    }

    std::swap(prv, nxt);
  }

  ll ans = INF;

  itr( it, prv ) {
    chmin( ans, it->second+llabs(it->first) );
  }

  std::cout << ans << endl;

  return 0;
}
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h> // __builtin_popcount(int x)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(x) int(x.size())
template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;}
template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;}
template <typename T> void print(T a) {cout << a << ' ';}
template <typename T> void printe(T a) {cout << a << endl;}
template <typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;}
template <typename T> void printp(pair<T, T> a) {print(a.first); cout<<a.second<<endl;}
template <typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);}
V<int> dx = {-1, 1,  0, 0, -1, -1,  1, 1};
V<int> dy = { 0, 0, -1, 1, -1,  1, -1, 1};

int main () {
    int n; cin >> n;
    V<P> a(n); // {l, r}
    const ll INF = 1e18;
    rep(i, n) a[i] = mp(INF, -INF);
    rep(i, n) {
        ll x, c; cin >> x >> c; c--;
        chmin(a[c].first, x), chmax(a[c].second, x);
    }

    V<ll> dp(2, 0); // 0:左 1:右
    ll l = 0, r = 0;
    rep(i, n) {
        if (a[i].first == INF) continue;
        V<ll> ndp(2);
        ll nl = a[i].first, nr = a[i].second;
        // l->nr->nl or r->nr->nl
        ndp[0] = min(dp[0] + abs(l-nr) + abs(nr-nl), dp[1] + abs(r-nr) + abs(nr-nl));
        // l->nl->nr or r->nl->nr
        ndp[1] = min(dp[0] + abs(l-nl) + abs(nl-nr), dp[1] + abs(r-nl) + abs(nl-nr));
        l = nl, r = nr;
        swap(dp, ndp);
    }
    ll ans = min(dp[0] + abs(l), dp[1] + abs(r));
    cout << ans << endl;
    return 0;
}