#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
using namespace std;

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

   long n;
   cin >> n;

   long *x=(long*)calloc(n,sizeof(long));
   long *y=(long*)calloc(n,sizeof(long));

   for (long i=0;i<n;i++)
   {
       long x1,y1;
       cin >> x1 >> y1;
       x[x1-1]++;
       y[y1-1]++;
   }
   long s=0;
   long brojac=0;
   for (long i=0;i<n;i++)
   {
       if (x[brojac]<2)
       while(brojac<n)
       {
           brojac ++;
           if (x[brojac]>=2)
            break;
       }
       if (x[i]==0)
       {
           x[i]++;
           x[brojac]--;
           s+=abs(brojac-i);
       }
   }
   brojac=0;
    for (long i=0;i<n;i++)
   {
       if (y[brojac]<2)
       while(brojac<n)
       {
           brojac ++;
           if (y[brojac]>=2)
            break;
       }
       if (y[i]==0)
       {
           y[i]++;
           y[brojac]--;
           s+=abs(brojac-i);
       }
   }
   cout << s;

    return 0;
}