using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
using Pair = System.Tuple<int, int>;
using Edge = System.Tuple<int, int, long>;
//using Debug;
//using static System.Globalization.CultureInfo;

class Program
{
    static void Main(string[] args)
    {
        Solve();
        //WriteLine(Solve());
    }
    static void Solve()
    {
        var num = Input.num;
        var xy = new int[num][];
        for(var i = 0; i < num; i++)
        {
            var inp = Input.ar;
            xy[i] = new[] { inp[0], inp[1], i };
        }
        var eg = new List<Edge>();
        var uf = UnionFind<int>.Make(num);
        for (var h = 0; h < 2; h++)
        {
            xy = xy.OrderBy(v => v[h]).ToArray();
            for (var i = 0; i < num - 1; i++)
                eg.Add(new Edge(xy[i][2], xy[i + 1][2], xy[i + 1][h] - xy[i][h]));
        }
        var ee = eg.OrderBy(v => v.Item3);
        var res = 0L;
        foreach (var e in ee)
            if(!uf.IsSame(e.Item1,e.Item2))
            {
                uf.Union(e.Item1, e.Item2);
                res += e.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 char[][] gred(int h) 
        => Enumerable.Repeat(0, h).Select(v => read.ToCharArray()).ToArray();
    public static int[][] ar2D(int num)
        => Enumerable.Repeat(0, num).Select(v => ar).ToArray();
    public static long[][] arL2D(int num)
        => Enumerable.Repeat(0, num).Select(v => arL).ToArray();
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}
