#include <iostream>
#include <map>
#include <vector>
#include <deque>
#include <algorithm>
#include <bitset>
#include <math.h>
#include <string>
#include <set>
#include <valarray>
#include <iomanip>
#include <bitset>
#include <random>
#include <complex>
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define pb push_back
#define ll long long
#define mp make_pair
#define fdeg firdeg
#define snd second
#define ld long double
#define mt make_tuple
#define fst firs
const double PI = acos(-1);
const int N = 5e6 + 17;
ll MOD = 1e9 + 7;
const int INF = 1e9 + 9;
const int MAXN = 26;
const int maxlog = 18;
ld eps = 1e-8;
random_device rd;
mt19937 rnd(rd());
uniform_int_distribution<ll> nextRand(0,(ll)1e9);
template<class A, class B>
void sum(A &a, B b)
{
    a += b;
    if (a < 0LL) a += MOD;
    if (a >= MOD) a -= MOD;
}
ll add(ll a, ll b)
{
    a += b;
    if (a < 0) a += MOD;
    if (a >= MOD) a -= MOD;
    return a;
}
int to_i(char c)
{
    return c - 'a';
}
ll mult(ll a, ll b)
{
    ll res = a * b - (ll) ((ld) a * b / MOD) * MOD;
    if (res < 0)
    {
        res += MOD;
    }
    if (res >= MOD)
    {
        res -= MOD;
    }
    return res;
}
ll gcd (ll a, ll b, ll & x, ll & y)
{
    if (a == 0)
    {
        x = 0LL; y = 1LL;
        return b;
    }
    ll x1, y1;
    ll d = gcd (b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}
long long poww(long long a, long long b)
{
    long long res = 1;
    a %= MOD;
    while (b > 0)
    {
        if (b % 2) res = mult(a, res);
        a = mult(a, a);
        b >>= 1;
    }
    return res % MOD;
}
ll inv(ll a)
{
    return poww(a , MOD - 2);
}
ll ans = 0 , ct = 0;
vector<int> l , r;
int dist(int a)
{
    return r[a] - l[a];
}
struct cmp
{
    bool operator()(const int& a, const int& b) const
    {
        if(l[a] > l[b]) return 1;
        if(l[a] < l[b]) return 0;
        if(l[a] == l[b])
        {
            if (dist(a) != dist(b))
                return dist(a) > dist(b);
        }
        return a < b;
    }
};
struct cmp_r
{
    bool operator()(const int& a, const int& b) const
    {
        if(r[a] < r[b]) return 1;
        if(r[a] > r[b]) return 0;
        if(r[a] == r[b])
        {
            if (dist(a) != dist(b))
                return dist(a) > dist(b);
        }
        return a < b;
    }
};
ll st = 0;
set<int , cmp> lp , lpc;
set<int , cmp_r> rp , rpc;
int n;
ll clc(int sd)
{
    lp = lpc;
    rp = rpc;
    ll ans2 = 0;
    ll cur = 0;
    ll x = 0;
    ct = 0;
    for(int i = 0 + sd; i < n + sd; i++)
    {
        // lp -> rp
        auto it = rp.begin();
        if(i % 2)
        {
            it = lp.begin();
        }
        cur = *it;
        //cout << cur << " ";
        lp.erase(cur);
        rp.erase(cur);
        if (x >= l[cur] && x <= r[cur])
            continue;
        if (x > r[cur])
        {
            ct += x - r[cur];
            x = r[cur];
        } else
        {
            ct += l[cur] - x;
            x = l[cur];
        }
    }
    if(x > 0)
    {
        ans2 = max(ct + x , ans2);
    }
    else ans2 = max(ct - x , ans2);
    return ans2;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));
    cin >> n;
    ll x = 0;
    for(int i = 0; i < n; i++)
    {
        int x , y;
        cin >> x >> y;
        l.pb(x);
        r.pb(y);
    }
    for(int i = 0; i < n; i++)
    {
        lp.insert(i);
        lpc.insert(i);
        rp.insert(i);
        rpc.insert(i);
    }
    ct = 0;
    ans = 0;
    int cur = 0;
//    cout << ct << " " << x << "\n";
//    cout << ans << "\n";
    for(int i = 0; i < 2; i++)
    {
        //cout <<  clc(i) << " ";
        ans = max(ans , clc(i));
    }
    cout << ans << "\n";
    return 0;
}


Measured memory_bytes: 21876