using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
using System.Numerics;

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            //Out.Flush();
            Read();
        }

        static void Solve()
        {
            var N = Sarray()[0];
            var X = new (long d, int i)[N];
            var Y = new (long d, int i)[N];
            for(var i=0;i<N;++i)
            {
                var (x, y) = Slice2<long>();
                X[i] = (x, i);
                Y[i] = (y, i);
            }
            X = X.OrderBy(x => x.d).ToArray();
            Y = Y.OrderBy(y => y.d).ToArray();
            var dsts = new (long d, int from, int to)[2 * (N - 1)];
            for (var i = 1; i < N; ++i)
            {
                dsts[i - 1] = (X[i].d - X[i - 1].d, X[i - 1].i, X[i].i);
                dsts[i - 1 + N - 1] = (Y[i].d - Y[i - 1].d, Y[i - 1].i, Y[i].i);
            }
            dsts = dsts.OrderBy(d => d.d).ToArray();
            var uf = new UnionFind((int)N);
            var ans = 0L;
            foreach (var d in dsts)
            {
                if (!uf.issame(d.from, d.to))
                {
                    uf.merge(d.from, d.to);
                    ans += d.d;
                }
            }
            WriteLine(ans);
        }
        class UnionFind
        {
            int[] par;
            public UnionFind(int n)
            {
                init(n);
            }
            public void init(int n)
            {
                par = new int[n];
                for (var i = 0; i < n; ++i) { par[i] = -1; }
            }
            public int root(int x)
            {
                if (par[x] < 0) return x;
                else return par[x] = root(par[x]);
            }
            public bool issame(int x, int y)
            {
                return root(x) == root(y);
            }
            public bool merge(int x, int y)
            {
                x = root(x);
                y = root(y);
                if (x == y) return false;
                if (par[x] > par[y])
                {
                    var tmp = y;
                    y = x;
                    x = tmp;
                }
                par[x] += par[y];
                par[y] = x;
                return true;
            }
            public int size(int x)
            {
                return -par[root(x)];
            }
        }


        static long Mod = (long)1e9 + 7;
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static public (T1 a, T2 b) Slice<T1, T2>()
        {
            var t = ReadLine().Split();
            return (
            (T1)Convert.ChangeType(t[0], typeof(T1)),
            (T2)Convert.ChangeType(t[1], typeof(T2)));
        }
        static public (T a, T b) Slice2<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)));
        }
        static public (T a, T b, T c) Slice3<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)),
            (T)Convert.ChangeType(t[2], typeof(T)));
        }
    }
}

Measured memory_bytes: 61944