#include <bits/stdc++.h>
using namespace std;

inline int closestZero(int* a, int n, int i) {
    int ci = -1;
    for(int j = i + 1; j < n; j++) {
        if(a[j] == 0) {
            ci = j;
            break;
        }
    }
    for(int j = i - 1; j >= 0; j--) {
        if(ci == -1 && a[j] == 0) {
            ci = j;
            break;
        }
        else if(a[j] == 0 && i - j < ci - i) {
            ci = j;
            break;
        }
    }
    return ci;
}

inline int numOfMoves(int* a, int n) {
    int ret = 0;
    for(int i = 0; i < n; i++) {
        while(a[i] > 1) {
            //cout << closestZero(a, n, i) << " ";
            ret += abs(closestZero(a, n ,i) - i);
            a[closestZero(a, n, i)] = 1;
            a[i]--;
        }
    }
    return ret;
}

int main() {
    int n;
    cin >> n;
    int* a = (int*)calloc(n, sizeof(int));
    int* b = (int*)calloc(n, sizeof(int));
    for(int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        a[x - 1]++; b[y - 1]++;
    }
    cout << numOfMoves(a, n) + numOfMoves(b, n) << endl;

    return 0;
}