using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
//using static System.Globalization.CultureInfo;

class Program
{
    class G
    {
        public int index;
        public int x;
        public int y;
        public G(int index, int x,int y)
        {
            this.index = index;
            this.x = x;
            this.y = y;
        }
    }
    static void Main(string[] args)
    {
        var num = Input.num;
        var gs = new G[num];
        for(var i = 0; i < num; i++)
        {
            var xy= Input.ar;
            gs[i] = new G(i, xy[0], xy[1]);
        }
        gs = gs.OrderBy(g => g.x).ToArray();
        var edge = new List<Tuple<int, int, long>>();
        for (var i = 1; i < num; i++)
            edge.Add(new Tuple<int, int, long>(gs[i - 1].index, gs[i].index, Min(Abs(gs[i - 1].x - gs[i].x), Abs(gs[i - 1].y - gs[i].y))));
        gs = gs.OrderBy(g => g.y).ToArray();
        for (var i = 1; i < num; i++)
            edge.Add(new Tuple<int, int, long>(gs[i - 1].index, gs[i].index, Min(Abs(gs[i - 1].x - gs[i].x), Abs(gs[i - 1].y - gs[i].y))));
        edge = edge.OrderBy(v => v.Item3).ToList();
        var uf = new UnionFind<int>();
        for (var i = 0; i < num; i++)
            uf.MakeSet(i);
        var res = 0L;
        foreach (var ed in edge)
            if (!uf.IsSame(ed.Item1, ed.Item2))
            {
                uf.Union(ed.Item2, ed.Item1);
                res += ed.Item3;
            }
        WriteLine(res);
    }
}

public class Input
{
    public static string read => ReadLine();
    public  static int[] ar => Array.ConvertAll(read.Split(' '), int.Parse);
    public  static int num => ToInt32(read);
    public static long[] arL => Array.ConvertAll(read.Split(' '), long.Parse);
    public  static long numL => ToInt64(read);
    public static string[] gred(int h) 
        => Enumerable.Repeat(0, h).Select(v => read).ToArray();
    public static int[][] twodim(int num)
        => Enumerable.Repeat(0, num).Select(v => ar).ToArray();
    public static long[][] twodimL(int num)
        => Enumerable.Repeat(0, num).Select(v => arL).ToArray();
    public const long Inf = (long)1e18;
    public const double eps = 1e-11;
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}
