using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zadatak3Takmicenje
{
    class Program
    {
        static bool arrayHasDup(int[] a)
        {
            Array.Sort(a);
            for (int i = 0; i < a.Length - 1; i++)
            {
                if (a[i] == a[i+1])
                {
                    return true;
                }
            }
            return false;
        }

        static int getDup(int[] a)
        {
            Array.Sort(a);
            for (int i = 0; i < a.Length - 1; i++)
            {
                if (a[i] == a[i + 1])
                {
                    return a[i];
                }
            }
            return -69;
        }

        static int find_first_empty(int[] a)
        {
            for (int i = 1; i <= a.Length; i++)
            {
                if (!Array.Exists(a, element => element == i))
                {
                    return i;
                }
            }
            return 0;
        }

        static bool isNumberDup(int[] a, int n)
        {
            int ponavljanja = 0;
            foreach (int num in a)
            {
                if (num == n)
                {
                    ponavljanja++;
                }
            }
            if (ponavljanja > 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        static void Main(string[] args)
        {
            //Console.WriteLine("daj n");
            int n = int.Parse(Console.ReadLine());
            int[] x = new int[n];
            int[] y = new int[n];
            int np = 0;
            int slot;
            int smallestD;
            int index = 0;

            for (int i = 0; i < n; i++)
            {
                string unos = Console.ReadLine();
                string[] brojevi = unos.Split(' ');
                x[i] = int.Parse(brojevi[0]);
                y[i] = int.Parse(brojevi[1]);
            }


            while (arrayHasDup(x))
            {
                slot = find_first_empty(x);
                smallestD = n;

                for (int i = 0; i < x.Length; i++)
                {
                    if (Math.Abs(slot - x[i]) < smallestD && isNumberDup(x, x[i])) //&& isDuplicate())
                    {
                        smallestD = Math.Abs(slot - x[i]);
                        index = i;
                    }
                }

                //Console.WriteLine("Za x");
                //Console.WriteLine("smallest distance is " + smallestD);
                //Console.WriteLine("index tog elementa je " + index + " i treba ga pomeriti " + (slot - x[index]));

                np += smallestD;
                x[index] += slot - x[index];
            }

            while (arrayHasDup(y))
            {
                slot = find_first_empty(y);
                smallestD = n;

                for (int i = 0; i < y.Length; i++)
                {
                    if (Math.Abs(slot - y[i]) < smallestD && isNumberDup(y, y[i])) //&& isDuplicate())
                    {
                        smallestD = Math.Abs(slot - y[i]);
                        index = i;
                    }
                }

                //Console.WriteLine("za y");
                //Console.WriteLine("smallest distance is " + smallestD);
                //Console.WriteLine("index tog elementa je " + index + " i treba ga pomeriti " + (slot - y[index]));

                np += smallestD;
                y[index] += slot - y[index];
            }
            Console.WriteLine(np);

            //foreach (int num in x)
            //{
            //    Console.Write(num + " ");
            //}

            //Console.WriteLine();

            //foreach (int num in y)
            //{
            //    Console.Write(num + " ");
            //}

            Console.ReadKey();
        }
    }
}