#include <bits/stdc++.h>

using namespace std;

int count_coins(vector<int> sums)
{
    int count = 0;
    deque<pair<int, int>> q;
    for(int i=0;i<sums.size();i++)
    {
        if(sums[i]>1)q.push_back(make_pair(i, sums[i]));
    }
    for(int i=0;i<sums.size();i++)
    {
        if (sums[i]==0)
        {
            int idx = q.front().first;
            int x = q.front().second;
            q.pop_front();
            count += abs(idx - i);
            if(x-1>1)q.push_front(make_pair(idx, x-1));
        }
    }
    return count;
}

int main()
{
    int n, r, c;
    cin >> n;
    vector<int> row_sum(n,0);
    vector<int> col_sum(n,0);
    for(int i=0;i<n;i++)
    {
        cin >> r;
        row_sum[r-1]++;
        cin >> c;
        col_sum[c-1]++;
    }
    // for(int x:row_sum)cout << x;
    // cout << endl;
    // for(int x:col_sum)cout << x;
    // cout << endl;
    int ans = count_coins(row_sum) + count_coins(col_sum);
    cout << ans;
    return 0;
}