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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = ReadInt();
            var point = new long[N][];
            for (int i = 0; i < N; i++)
            {
                var input = ReadArrayLong();
                point[i] = new long[3] { input[0], input[1], i };
            }
            var costs = new List<long[]>();
            point = point.OrderBy(x => x[0]).ToArray();
            for (int i = 0; i < N; i++)
            {
                if (i == 0)
                {
                    costs.Add(new long[] { point[i][2], point[i + 1][2], point[i + 1][0] - point[i][0] });
                }
                else if (i == N - 1)
                {
                    costs.Add(new long[] { point[i - 1][2], point[i][2], point[i][0] - point[i - 1][0] });
                }
                else
                {
                    costs.Add(new long[] { point[i][2], point[i + 1][2], point[i + 1][0] - point[i][0] });
                    costs.Add(new long[] { point[i - 1][2], point[i][2], point[i][0] - point[i - 1][0] });
                }
            }
            point = point.OrderBy(y => y[1]).ToArray();
            for (int i = 0; i < N; i++)
            {
                if (i == 0)
                {
                    costs.Add(new long[] { point[i][2], point[i + 1][2], point[i + 1][1] - point[i][1] });
                }
                else if (i == N - 1)
                {
                    costs.Add(new long[] { point[i - 1][2], point[i][2], point[i][1] - point[i - 1][1] });
                }
                else
                {
                    costs.Add(new long[] { point[i][2], point[i + 1][2], point[i + 1][1] - point[i][1] });
                    costs.Add(new long[] { point[i - 1][2], point[i][2], point[i][1] - point[i - 1][1] });
                }
            }
            costs = costs.Distinct().OrderBy(c => c[2]).ToList();
            var uf = new UnionFind(N);
            var ans = 0L;
            foreach(var c in costs)
            {
                if (!uf.Same(c[0], c[1]))
                {
                    uf.Unite(c[0], c[1]);
                    ans += c[2];
                }
            }

            Console.WriteLine(ans);
            Console.ReadLine();
        }

        static string Read() { return Console.ReadLine(); }
        static int ReadInt() { return int.Parse(Read()); }
        static long ReadLong() { return long.Parse(Read()); }
        static int[] ReadArrayInt() { return Read().Split(' ').Select(s => int.Parse(s)).ToArray(); }
        static long[] ReadArrayLong() { return Read().Split(' ').Select(s => long.Parse(s)).ToArray(); }
    }

    class UnionFind
    {
        long[] par;
        long[] rank;

        public UnionFind(long n)
        {
            par = new long[n];
            rank = new long[n];
            for (var i = 0L; i < n; i++)
            {
                par[i] = i;
                rank[i] = 0;
            }
        }

        public long Root(long x)
        {
            return par[x] == x ? x : par[x] = Root(par[x]);
        }

        public bool Same(long x, long y)
        {
            return Root(x) == Root(y);
        }

        public void Unite(long x, long y)
        {
            x = Root(x);
            y = Root(y);
            if (x == y) return;

            if (rank[x] < rank[y])
            {
                par[x] = y;
            }
            else
            {
                par[y] = x;
                if (rank[x] == rank[y]) rank[x]++;
            }
        }
    }
}

Measured memory_bytes: 82576