#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
  o << "(" << v.first << "," << v.second << ")";
  return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
  for (auto e : v) {
    o << e << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

#if 1

struct E {
  int l = INT32_MAX, h = INT32_MIN;
};

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N; cin >> N;
  v(E) es(N);
  F0R(i, N) {
    int x, c; cin >> x >> c;
    c--;
    chmin(es[c].l, x);
    chmax(es[c].h, x);
  }
  ll lx = 0, lt = 0;
  ll rx = 0, rt = 0;
  F0R(c, N) {
    auto [l, r] = es[c];
    if (l == INT32_MAX) continue;
    ll rx2 = es[c].h, lx2 = es[c].l;
    ll rt2 = min(lt + abs(lx - l) + abs(l - r), rt + abs(rx - l) + abs(l - r));
    ll lt2 = min(lt + abs(lx - r) + abs(r - l), rt + abs(rx - r) + abs(r - l));

    lx = lx2; rx = rx2;
    lt = lt2; rt = rt2;
  }
  cout << min(lt+abs(lx), rt+abs(rx)) << newl;
}
#endif

#include<bits/stdc++.h>
#define mod 998244353
#define INF LLONG_MAX
#define lli long long int
using namespace std;
#define PII pair<int, int>
#define PLL pair<lli, lli>
#define pub push_back
#define F first
#define S second
#define fastIO ios::sync_with_stdio(0); cin.tie(0);

int main() {
    //fastIO
    //freopen("IN.txt", "r", stdin);
    int n; cin>>n;
    vector<pair<int, int>> arr(n, {INT_MAX, INT_MIN});
    for(int i = 0; i<n; i++){
        int x, c; cin>>x>>c;
        arr[c-1].first = min(arr[c-1].first, x);
        arr[c-1].second = max(arr[c-1].second, x);
    }
    vector<vector<lli>> dp(n, vector<lli>(2));
    lli pos = 0, pos1 = 0;
    lli cost = 0, cost1 = 0;
    for(int i = 0; i<n; i++){
        if(arr[i].first == INT_MAX && arr[i].second == INT_MIN){
            continue;
        }
        dp[i][0] = min(abs(pos-arr[i].first) + cost, abs(pos1-arr[i].first) + cost1) + (arr[i].second-arr[i].first);
        //if(i != 0) dp[i][0] += dp[i-1][1];
        //if(i != 0) pos = arr[i-1].first;
        dp[i][1] = min(abs(pos-arr[i].second) + cost, abs(pos1-arr[i].second) + cost1) + (arr[i].second-arr[i].first);
        //if(i != 0) dp[i][1] += dp[i-1][0];
        cost = dp[i][0]; cost1 = dp[i][1];
        pos = arr[i].second; pos1 = arr[i].first;
    }
    cout<<min(cost + abs(pos), cost1 + abs(pos1))<<"\n";
    return 0;
}
