#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
using namespace std;

#define _CRT_SECURE_NO_WARNINGS
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define re_rep(i, n) for (int i = (n); i >= 1;)
#define wrep(a) while ((int)a > 0)
#define re_wrep(a, N) while ((int)a < N)
#define scanfd(N) scanf("%d", &N);
#define scanfc(a) scanf("%s", a);
#define scanfca(a) scanf("%s", a);
#define scanfd2(a, b) scanf("%d %d", &a, &b);
#define scanfca2(a, b) scanf("%s%s", a, b);
#define scanfcad(a, b) scanf("%s%d", a, &b);
#define scanfc3(a, b, c) scanf("%s%s%s", &a, &b, &c);
#define scanfc3a(a, b, c) scanf("%s%s%s", a, b, c);
#define scanfdcd(a, b, c) scanf("%d %c %d", &a, &b, &c);
#define scanfsd_if(c, d) scanf("%s %d", &c, &d);
#define scanfcd_switch(c, d) scanf(" %c %d", &c, &d);
#define printfd(a) printf("%d", a);
#define printfca(a) printf("%s", a);
#define printfc3(a, b, c) printf("%c%c%c", a, b, c);

struct MyPair {
    string S;
    int T;
};

bool t_cmp(const struct MyPair &p, const struct MyPair &q) { return p.T < q.T; }

int main() {
    int N, T;
    string S, a;

    cin >> N;
    vector<MyPair> p(N);

    rep(i, N) {
        cin >> S >> T;
        p.at(i).S = S;
        p.at(i).T = T;
    }
    sort(p.begin(), p.end(), t_cmp);

    // rep(i, N) { cout << p.at(i).S << endl; }
    cout << p.at(N - 2).S << endl;
    return 0;
}
#ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/extc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)

/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int big=1000000007;
//const int big=998244353;
const double EPS=1e-8; //適宜変える
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
    constexpr operator int(){return -int(1e9)-10;}
    constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
    constexpr operator int(){return int(1e9)+10;}
    constexpr operator ll(){return ll(1e18)+10;}
    constexpr auto operator -(){return neginf;}
}inf;

/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif

/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}



int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.precision(10);
/*------------------------------------*/
    
    int n;cin >> n;
    vector<pair<ll,ll>> xc(n); 
    vector<ll> x(n),c(n);
    rep(i,n){
        cin >> xc[i].first >> xc[i].second;
    }
    sort(ALL(xc));
    rep(i,n){
        x[i]=xc[i].first;
        c[i]=xc[i].second, c[i]--;
    }
    vector<vector<int>> col(n);
    rep(i,n){
        col[c[i]].emplace_back(i);
    }
    dbg(x);
    dbg(c);
    ll x_l=0;
    ll x_r=0;
    ll val_l=0;
    ll val_r=0;
    rep(i,n){
        if(col[i].size()){
            ll ne_l=x[col[i].front()];
            ll ne_r=x[col[i].back()];
            ll nval_l=inf;
            ll nval_r=inf;
            chmin(nval_l, val_l+abs(x_l-ne_r)+abs(ne_r-ne_l));
            chmin(nval_l, val_r+abs(x_r-ne_r)+abs(ne_r-ne_l));

            chmin(nval_r, val_l+abs(x_l-ne_l)+abs(ne_r-ne_l));
            chmin(nval_r, val_r+abs(x_r-ne_l)+abs(ne_r-ne_l));

            x_l=ne_l;
            x_r=ne_r;
            val_l=nval_l;
            val_r=nval_r;
            dbg(x_l, val_l);
            dbg(x_r,val_r);
            dbgnewl;
        }
    }
    cout << min(val_l+abs(x_l), val_r+abs(x_r)) << newl;
}