#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N=(int) 2e5+99;
int n;
int r[N], k[N];
queue<pair<int, int> > pr;
queue<int> dr;
ll solve(int niz[]) {
    ll sol = 0;
    for(int i=0; i<n; ++i) {
        if(niz[i]>=2) pr.emplace(make_pair(i, niz[i]-1));
        else if(niz[i]==0) dr.emplace(i);
    }
    /*cout << endl;
    while(!pr.empty()) {cout << pr.front().first << ' ' << pr.front().second << endl; pr.pop();}
    cout << endl;
    while(!dr.empty()) {cout << dr.front() << endl; dr.pop();}*/
    while(!dr.empty()) {
        sol += ll(abs(pr.front().first-dr.front()));
        dr.pop();
        --pr.front().second;
        if(pr.front().second==0) pr.pop();
    }
    return sol;
}
int main() {
    IOS; cin >> n;
    for(int i=0; i<n; ++i)
    {int a, b; cin >> a >> b; ++r[--a]; ++k[--b];}
    ll res = 0;
    res += solve(r);
    res += solve(k);
    cout << res;
    return 0;
}