#include <iostream>
#include<stdio.h>
#include<stdio.h>
using namespace std;
int r[1000000],c[1000000],a[1000000];

void heapify(int arr[], int n, int i)
{
    int largest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;

    if (l < n && arr[l] > arr[largest])
        largest = l;


    if (r < n && arr[r] > arr[largest])
        largest = r;


    if (largest != i) {
        swap(arr[i], arr[largest]);


        heapify(arr, n, largest);
    }
}


void heapSort(int arr[], int n)
{

    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);


    for (int i = n - 1; i >= 0; i--) {

        swap(arr[0], arr[i]);


        heapify(arr, i, 0);
    }
}



int main()
{
    int n,i,br=0,d,l,m;
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&r[i]);
        scanf("%d",&c[i]);
    }
    heapSort(r, n);
    heapSort(c, n);
    l=0;
    d=n-1;
    for(i=0;i<n;i++){
    while (l <= d) {
        int m = l + (d - l) / 2;


        if (r[m] == i+1)
            {swap(r[i],r[m]);
            break;
            }

        if (r[m] < i+1)
            l = m + 1;


        else
            d = m - 1;
    }
    }

    l=0;
    d=n-1;



    for(i=0;i<n;i++){
    while (l <= d) {
        int m = l + (d - l) / 2;


        if (c[m] == i+1)
            {swap(c[m],c[i]);
            break;
            }

        if (c[m] < i+1)
            l = m + 1;


        else
            d = m - 1;
    }
    }


    for(i=0;i<n;i++)
    {

    while(r[i]>i+1){
        r[i]--;
        br++;
    }

    while(r[i]<i+1){
        r[i]++;
        br++;
    }


    }



    for(i=0;i<n;i++)
    {
        while(c[i]>i+1){c[i]--;
    br++;
    }
     while(c[i]<i+1){c[i]++;
    br++;
    }
    }
    printf("%d",br);
    return 0;
}