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

namespace ConsoleApplication3
{
    class Program
    {
        static void provera(int[] a, ref int t, ref int zbir, int r)
        {
            bool p = true;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == t)
                    p = false;
            }
            if (p == false)
            {
                t++;
                provera(a, ref t, ref zbir, r);
            }
            else
            {
                promena(a, t, r, ref zbir);
            }
        }
        static void promena(int[] a, int t, int r, ref int zbir)
        {
            zbir += Math.Abs(a[r] - t);
            a[r] = t;
        }
        static void Main(string[] args)
        {
            int t = 1, k = 1, zbir = 0;
            int n = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[n];
            int[] b = new int[n];
            for (int i = 0; i < n; i++)
            {
                string[] p = Console.ReadLine().Split();
                a[i] = Convert.ToInt32(p[0]);
                b[i] = Convert.ToInt32(p[1]);
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (a[i] == a[j] && i != j)
                        provera(a, ref t, ref zbir, j);
                    if (b[i] == b[j] && i != j)
                        provera(b,ref k,ref zbir, j);
                }
            } 
            Console.WriteLine(zbir);
            Console.ReadLine();
        }
    }
}