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


namespace Zlatnici_Takmicenje
{
    class Program
    {
        public class Tacka
        {
            public int x, y;
            public bool xp, yp;
            public Tacka()
            {
                x = 0;
                y = 0;
                xp = false;
                yp = false;   
            }
        };

        static void Main(string[] args)
        {
            string[] tokens = Console.ReadLine().Split();
            int n = int.Parse(tokens[0]);
            int p = 0;
            int r = 0;
            bool[] bx = new bool[n];
            bool[] by = new bool[n];
            Tacka[] a = new Tacka[n];
            int[,] m = new int[2, n];
            for(int i = 0; i < n; i++)
            {
                a[i] = new Tacka();
                string[] token = Console.ReadLine().Split();
                a[i].y = Convert.ToInt32((token[0]))-1;
                 a[i].x = Convert.ToInt32((token[1])) - 1;  
            }
            for(int i= 0; i < n; i++)
            {
                bx[a[i].x] = true;
                by[a[i].y] = true;
                for(int j = 0; j < n; j++)
                {
                    if ((a[i].x == a[j].x) && (i != j))
                    {
                        a[i].xp = true;
                        a[j].xp = true;
                    }
                    if((a[i].y == a[j].y)  && (i != j))
                    {
                        a[i].yp = true;
                        a[j].yp = true;
                    }
                }
            }
            // Unos  ^^^^^ svih stvari RADI
            
            for(int i = 0; i < n; i++)
            {
                if (!bx[i])
                {
                    for(int j = 0; j < n; j++)
                    {
                        m[0, j] = Math.Abs(a[j].x - i);
                        m[1, j] = j;
                    }
                //Ovo gore radi
                    for (int o = 0; o < n; o++)
                    {
                        for (int j = o + 1; j < n; j++)
                        {
                            if (m[0, o] > m[0, j])
                            {
                            p = m[0, o];
                            m[0, o] = m[0, j];
                            m[0, j] = p;
                            p = m[1, o];
                            m[1, o] = m[1, j];
                            m[1, j] = p;
                            }
                        }
                    }
                    //Sort radi
                    for (int j = 0; j < n; j++)
                    {
                        if (a[m[1, j]].xp)
                        {
                            a[m[1,j]].xp = false;                           
                            bx[i] = true;
                            r += m[0,j];
                            a[m[1, j]].x = i;

                            for (int o = 0; o < n; o++)
                            {
                                bx[a[o].x] = true;
                                
                                for (int u = 0; u < n; u++)
                                {
                                    if ((a[o].x == a[u].x) && (o != u))
                                    {
                                        a[o].xp = true;
                                        a[u].xp = true;
                                    }
                                    
                                }
                            }
                            break;
                           
                        }
                    }
                }
            }
            //////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < n; i++)
            {
                if (!by[i])
                {
                    for (int j = 0; j < n; j++)
                    {
                        m[0, j] = Math.Abs(a[j].y - i);
                        m[1, j] = j;
                    }
                    //Ovo gore radi
                    for (int o = 0; o < n; o++)
                    {
                        for (int j = o + 1; j < n; j++)
                        {
                            if (m[0, o] > m[0, j])
                            {
                                p = m[0, o];
                                m[0, o] = m[0, j];
                                m[0, j] = p;
                                p = m[1, o];
                                m[1, o] = m[1, j];
                                m[1, j] = p;
                            }
                        }
                    }
                    //Sort radi
                    for (int j = 0; j < n; j++)
                    {
                        if (a[m[1, j]].yp)
                        {
                            a[m[1, j]].yp = false;
                            by[i] = true;
                            r += m[0, j];
                            a[m[1, j]].y = i;

                            for (int o = 0; o < n; o++)
                            {
                               
                                by[a[o].y] = true;
                                for (int u = 0; u < n; u++)
                                {
                                  
                                    if ((a[o].y == a[u].y) && (o != u))
                                    {
                                        a[o].yp = true;
                                        a[u].yp = true;
                                    }
                                }
                            }
                            break;

                        }
                    }
                }
            }

            ////////////////////////////////////////////////////////////////////////////
            Console.WriteLine("{0}",r);
            
        }
    }
}