#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e8)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int main()
{
	owo
	int n;
	cin>>n;
	vector<ll>a(2*n);
	for(int i=0;i<2*n;i++)cin>>a[i];
	vector<int>id(2*n);
	string ans(2*n,'?');
	for(int i=0;i<2*n;i++)id[i] = i;
	sort(all(id),[&](int x,int y){return a[x] > a[y];});
	vector<bool>beeg(2*n);
	for(int i=0;i<n;i++)beeg[id[i]] = 1;
	stack<int>stk;
	for(int i=0;i<2*n;i++){
		if(stk.empty() || beeg[stk.top()] == beeg[i])stk.push(i);
		else{
			ans[stk.top()] = '(';
			ans[i] = ')';
			stk.pop();
		}
	}
	cout<<ans<<'\n';
	
}

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int _ = 1;

ll dp[200001][2];
map<ll, ll>mn;
map<ll, ll>mx;
vector<int>colors;

ll solve(int i , int left){
    ll &ret = dp[i][left];
    if(~ret)return ret;
    ret = 1e16;
    ll inter = abs(mx[colors[i]] - mn[colors[i]]);
    if(i == colors.size()-1){
        if(left)ret = inter + abs(mx[colors[i]]);
        else ret = inter + abs(mn[colors[i]]);
    }else{
        if(left){
            ret = min(ret, inter + abs(mx[colors[i]] - mx[colors[i+1]]) + solve(i+1, 0));
            ret = min(ret, inter + abs(mx[colors[i]] - mn[colors[i+1]]) + solve(i+1, 1));
        }else{
            ret = min(ret, inter + abs(mn[colors[i]] - mx[colors[i+1]]) + solve(i+1, 0));
            ret = min(ret, inter + abs(mn[colors[i]] - mn[colors[i+1]]) + solve(i+1, 1));
        }
    }
    return ret;
}


void _solve() {
    memset(dp,-1,sizeof(dp));
    int n;
    cin>>n;
    for(int i = 0 ; i < n ; i++) {
        ll x,c;
        cin>>x>>c;
        if(mn.find(c) != mn.end()){
            mn[c] = min(mn[c] , x);
            mx[c] = max(mx[c] , x);
        }else{
            mn[c] = x;
            mx[c] = x;
        }
    }
    colors.clear();
    for(auto x: mx){
        colors.push_back(x.first);
    }
    cout<< min(solve(0,0) + abs(mx[colors[0]]) , solve(0,1) + abs(mn[colors[0]]))<<'\n';
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    while (_--)_solve();
    return 0;
}