#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

void Main() {
  ll n;
  R n;
  P a[n];
  rep(i,n) cin >> a[i].F >> a[i].S;
  map<ll,P> m;
  rep(i,n) {
    if(!m.count(a[i].S)) m[a[i].S]=P(a[i].F,a[i].F);
    m[a[i].S].F=min(m[a[i].S].F,a[i].F);
    m[a[i].S].S=max(m[a[i].S].S,a[i].F);
  }
  vector<P> p;
  p.pb(P(0,0));
  tr(it,m) {
    P q=it->S;
    p.pb(P(q.F,q.S));
  }
  p.pb(P(0,0));
  n=p.size();
  ll dp[n][2];
  rep(i,n)rep(j,2)dp[i][j]=MAXL;
  dp[0][0]=dp[0][1]=0;
  REP(i,1,n) {
    dp[i][0]=min(dp[i][0],dp[i-1][0]+abs(p[i-1].F-p[i].S)+abs(p[i].F-p[i].S));
    dp[i][0]=min(dp[i][0],dp[i-1][1]+abs(p[i-1].S-p[i].S)+abs(p[i].F-p[i].S));
    dp[i][1]=min(dp[i][1],dp[i-1][0]+abs(p[i-1].F-p[i].F)+abs(p[i].F-p[i].S));
    dp[i][1]=min(dp[i][1],dp[i-1][1]+abs(p[i-1].S-p[i].F)+abs(p[i].F-p[i].S));
  }
  pr(dp[n-1][0]);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cerr << #x << " = " << x << endl
#define boost ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) { cout << endl; }

template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << " ";
    err(++it, args...);
}

template<typename T>
inline void read(T& x) {
    x = 0; T f = 1; char ch = getchar();
    while (!isdigit(ch)) f = (ch == '-') ? -1 : 1, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
    x *= f;
}

template<typename T, typename ...Args>
inline void read(T& x, Args&... args) { read(x), read(args...); }

constexpr int MAXN = 2e5 + 5, MOD = 1e9 + 7;
#define int long long

int n;
array<long long, MAXN> x, c, l, r;
long long dp[MAXN][2];

signed main() {
    boost;
    cin >> n;
    vector<int> vc;
    memset(dp, 0x3f, sizeof(dp));
    fill(l.begin(), l.end(), 2e9);
    fill(r.begin(), r.end(), -2e9);
    for (int i = 1; i <= n; i++) {
        cin >> x[i] >> c[i];
        l[c[i]] = min(l[c[i]], x[i]);
        r[c[i]] = max(r[c[i]], x[i]);
        vc.push_back(c[i]);
    }
    sort(vc.begin(), vc.end());
    int cnt = unique(vc.begin(), vc.end()) - vc.begin();
    for (int i = 0; i < cnt; i++) {
        int co = vc[i], L = l[co], R = r[co], prePos = 0;
        if (i == 0) {
            if (R * L < 0) {
                dp[0][0] = R * 2 - L; // stop at left
                dp[0][1] = 2LL * (-L) + R;
            } else {
                if (R > 0) {
                    dp[0][0] = 2LL * R - L;
                    dp[0][1] = R;
                } else {
                    dp[0][0] = -L;
                    dp[0][1] = 2LL * (-L) - R;
                }
            }
        } else {
            prePos = l[vc[i - 1]];
            if ((R - prePos) * (L - prePos) < 0) {
                dp[i][0] = min(dp[i][0], dp[i - 1][0] + (R - prePos) * 2 - (L - prePos));
                dp[i][1] = min(dp[i][1], dp[i - 1][0] + 2LL * (-(L - prePos)) + (R - prePos));
            } else {
                if ((R - prePos) > 0) {
                    dp[i][0] = min(dp[i][0], dp[i - 1][0] + 2LL * (R - prePos) - (L - prePos));
                    dp[i][1] = min(dp[i][1], dp[i - 1][0] + R - prePos);
                } else {
                    dp[i][0] = min(dp[i][0], dp[i - 1][0] + (-(L - prePos)));
                    dp[i][1] = min(dp[i][1], dp[i - 1][0] + 2LL * (-(L - prePos)) - (R - prePos));
                }
            }

            prePos = r[vc[i - 1]];
            if ((R - prePos) * (L - prePos) < 0) {
                dp[i][0] = min(dp[i][0], dp[i - 1][1] + (R - prePos) * 2 - (L - prePos));
                dp[i][1] = min(dp[i][1], dp[i - 1][1] + 2LL * (-(L - prePos)) + (R - prePos));
            } else {
                if ((R - prePos) > 0) {
                    dp[i][0] = min(dp[i][0], dp[i - 1][1] + 2LL * (R - prePos) - (L - prePos));
                    dp[i][1] = min(dp[i][1], dp[i - 1][1] + R - prePos);
                } else {
                    dp[i][0] = min(dp[i][0], dp[i - 1][1] + (-(L - prePos)));
                    dp[i][1] = min(dp[i][1], dp[i - 1][1] + 2LL * (-(L - prePos)) - (R - prePos));
                }
            }
        }
    }
    cout << min(dp[cnt - 1][0] + abs(l[vc.back()]), dp[cnt - 1][1] + abs(r[vc.back()]));
    return 0;
}