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
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static long Mod = (long)1e9 + 7;
        static void Main(string[] args)
        {
            SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            Out.Flush();
            Read();
        }
        static void Solve()
        {
            var N = Sarray()[0];
            var points = new List<(int i, int x, int y)>();
            for (var i = 0; i < N; ++i)
            {
                var xy = Sarray();
                points.Add((i, (int)xy[0], (int)xy[1]));
            }
            points.Sort((l, r) => l.x - r.x);
            var pq = new priority_queue<(int dst, int f, int t)>((l, r) => r.dst - l.dst);
            for (var i = 0; i < N - 1; ++i)
                pq.insert((points[i + 1].x - points[i].x, points[i].i, points[i + 1].i));
            points.Sort((l, r) => l.y - r.y);
            for (var i = 0; i < N - 1; ++i)
                pq.insert((points[i + 1].y - points[i].y, points[i].i, points[i + 1].i));
            var cst = 0L;
            var uf = new UnionFind((int)N);
            while (pq.num != 0)
            {
                var dft = pq.extract();
                if (uf.issame(dft.f, dft.t)) continue;
                uf.merge(dft.f, dft.t);
                cst += dft.dst;
            }
            WriteLine(cst);
        }
        class priority_queue<T>
        {
            Comparison<T> cmp;
            T[] A = new T[300000];
            // var pq = new priority_queue<long>((l, r) => (int)(l - r));
            // とすれば値の大きいものから優先
            public priority_queue(Comparison<T> c)
            {
                cmp = c;
            }
            public int num { get; private set; } = 0;
            public void insert(T v)
            {
                A[num] = v;
                ++num;
                increaseKey(num);
            }
            void increaseKey(int i)
            {
                while (1 < i && 0 < cmp(A[i - 1], A[i / 2 - 1]))
                {
                    var t = A[i - 1];
                    A[i - 1] = A[i / 2 - 1];
                    A[i / 2 - 1] = t;
                    i = i / 2;
                }
            }
            public T extract()
            {
                if (num < 1) return default(T);
                var max = A[0];
                A[0] = A[num - 1];
                --num;
                maxHeapify(1);
                return max;
            }
            void maxHeapify(int i)
            {
                var H = num;
                var l = left(i);
                var r = right(i);
                var largest = 0;
                if (l <= H && 0 < cmp(A[l - 1], A[i - 1])) { largest = l; } else { largest = i; }
                if (r <= H && 0 < cmp(A[r - 1], A[largest - 1])) { largest = r; }
                if (largest != i)
                {
                    var t = A[i - 1];
                    A[i - 1] = A[largest - 1];
                    A[largest - 1] = t;
                    maxHeapify(largest);
                }
            }
            int left(int l) => l * 2;
            int right(int r) => r * 2 + 1;
        }
        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)];
            }
        }
    }
}

Measured memory_bytes: 53364