#include <bits/stdc++.h>

using namespace std;

struct Tacka
{
    int i, j;
};

bool sort_i(const Tacka &t, const Tacka &t1)
{
    return t.i < t1.i;
}

bool sort_j(const Tacka &t, const Tacka &t1)
{
    return t.j < t1.j;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    Tacka kord[n];

    map<int, bool> i_brojevi, j_brojevi;
    for(int i = 1; i <= n; ++i)
    {
        i_brojevi.insert(pair<int, bool>(i, false));
        j_brojevi.insert(pair<int, bool>(i, false));
    }

    for(int i = 0; i < n; ++i)
    {
        cin >> kord[i].i >> kord[i].j;
        i_brojevi[kord[i].i] = true;
        j_brojevi[kord[i].j] = true;
    }

    int total = 0;

    sort(kord, kord + n, sort_i);
    for (int i = 1; i < n; ++i)
    {
        if (kord[i-1].i == kord[i].i)
        {
            for(map<int, bool>::iterator itr = i_brojevi.begin(); itr != i_brojevi.end(); ++itr)
            {
                if(!(itr->second))
                {
                    total += abs(kord[i].i - itr->first);
                    i_brojevi[itr->first] = true;
                }
            }
        }
    }

    sort(kord, kord + n, sort_j);
    for (int i = 1; i < n; ++i)
    {
        if (kord[i-1].j == kord[i].j)
        {
            for(map<int, bool>::iterator itr = j_brojevi.begin(); itr != j_brojevi.end(); ++itr)
            {
                if(!(itr->second))
                {
                    total += abs(kord[i].j - itr->first);
                    j_brojevi[itr->first] = true;
                }
            }
        }
    }

    cout << total;

    return 0;
}