#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#define ll long long

using namespace std;

int main()
{
    ll n, x, y, ind1, ind2, res1, res2;
    ind1 = ind2 = res1 = res2 = 0;
    bool b = false;
    cin >> n;

    vector<ll>prvi;
    vector<ll>drugi;
    map<ll, ll>m1;
    map<ll, ll>m2;

    for (ll i = 0; i < n; i++)
    {
        cin >> x >> y;

        m1[x]++;
        m2[y]++;

        if (m1[x] == 2)
        {
            prvi.push_back(x);
            b = true;
        }

        if (m2[y] == 2)
        {
            drugi.push_back(y);
            b = true;
        }
    }

    sort(prvi.begin(), prvi.end());
    sort(drugi.begin(), drugi.end());

    if (b)
    {
        for (ll i = 1; i <= n; i++)
        {
            if (m1[i] == 0)
            {
                res1 += abs(prvi[ind1] - i);
                m1[prvi[ind1]]--;

                if (m1[prvi[ind1]] == 1)
                    ind1++;
            }

            if (m2[i] == 0)
            {
                res2 += abs(drugi[ind2] - i);
                m2[drugi[ind2]]--;

                if (m2[drugi[ind2]] == 1)
                    ind2++;
            }
        }
    }

    cout << res1 + res2;


    return 0;
}