using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace ABC065D
{
    class Program
    {
        public static void Solve(Input input)
        {
            var n = input.NextInt();
            var node = new Tuple<int, int, int>[n];

            for (int i = 0; i < n; i++)
            {
                var x = input.NextInt() - 1;
                var y = input.NextInt() - 1;

                node[i] = new Tuple<int, int, int>(i, x, y);
            }

            var k = new Kruskal(n);

            Array.Sort(node, (x, y) => x.Item2.CompareTo(y.Item2));
            for (int i = 0; i < n - 1; i++)
            {
                var cost = Math.Min(Math.Abs(node[i].Item2 - node[i + 1].Item2)
                    , Math.Abs(node[i].Item3 - node[i + 1].Item3));

                k.AddPath(node[i].Item1, node[i + 1].Item1, cost);
            }

            Array.Sort(node, (x, y) => x.Item3.CompareTo(y.Item3));
            for (int i = 0; i < n - 1; i++)
            {
                var cost = Math.Min(Math.Abs(node[i].Item2 - node[i + 1].Item2)
                    , Math.Abs(node[i].Item3 - node[i + 1].Item3));

                k.AddPath(node[i].Item1, node[i + 1].Item1, cost);
            }

            var path = k.MinimumSpanningTree();

            Console.WriteLine(path.Sum(x => x.Cost));
        }

        /// <summary>
        /// 最小全域木(Minimum Spanning Tree)を求める
        /// 無向グラフのみ
        /// 有向グラフは「Chu-Liu/Edmondsのアルゴリズム」とからしい
        /// </summary>
        class Kruskal
        {
            /// <summary>
            /// アイテムを含まないUF木
            /// </summary>
            class UnionFind
            {
                List<int> rank { get; set; }
                List<int> size { get; set; }
                List<int> parentId { get; set; }

                public UnionFind(int size)
                {
                    this.rank = Enumerable.Repeat(0, size).ToList();
                    this.size = Enumerable.Repeat(1, size).ToList();
                    this.parentId = Enumerable.Range(0, size).ToList();
                }

                /// <summary>
                /// 同一の親を持つか
                /// </summary>
                /// <param name="x"></param>
                /// <param name="y"></param>
                /// <returns></returns>
                public bool Same(int x, int y)
                {
                    return FindRoot(x) == FindRoot(y);
                }

                /// <summary>
                /// 指定ノードのrootを返す
                /// </summary>
                /// <param name="x"></param>
                /// <returns></returns>
                public int FindRoot(int x)
                {
                    if (x != this.parentId[x])
                    {
                        // 経路中のノードのparentを更新しておく
                        this.parentId[x] = FindRoot(this.parentId[x]);
                    }

                    return this.parentId[x];
                }

                /// <summary>
                /// 重みをつけて合体する
                /// </summary>
                /// <param name="x"></param>
                /// <param name="y"></param>
                /// <param name="w"></param>
                public void Merge(int x, int y)
                {
                    x = FindRoot(x);
                    y = FindRoot(y);

                    if (x == y) return;

                    if (this.rank[y] < this.rank[x])
                    {
                        this.parentId[y] = x;
                        this.size[x] += this.size[y];
                    }
                    else
                    {
                        this.parentId[x] = y;
                        this.size[y] += this.size[x];

                        if (this.rank[x] == this.rank[y])
                        {
                            this.rank[y]++;
                        }
                    }
                }

                /// <summary>
                /// xと同じ親を持つノードの数
                /// </summary>
                /// <param name="x"></param>
                /// <returns></returns>
                public int Size(int x)
                {
                    x = FindRoot(x);

                    return this.size[x];
                }
            }

            public struct Edge : IComparable<Edge>
            {
                public int v1;
                public int v2;
                public int Cost;

                public Edge(int v1, int v2, int cost)
                {
                    this.v1 = v1;
                    this.v2 = v2;
                    this.Cost = cost;
                }

                public int CompareTo(Edge other)
                {
                    return this.Cost - other.Cost;
                }
            }

            static readonly int INF = int.MaxValue / 5;

            // 接続情報
            List<Edge> es;
            // ノード数
            int V;

            /// <summary>
            /// 
            /// </summary>
            /// <param name="v">ノード数</param>
            public Kruskal(int v)
            {
                es = new List<Edge>();
                V = v;
            }

            /// <summary>
            /// 経路追加
            /// </summary>
            /// <param name="v1"></param>
            /// <param name="v2"></param>
            /// <param name="cost"></param>
            public void AddPath(int v1, int v2, int cost)
                => es.Add(new Edge(v1, v2, cost));

            /// <summary>
            /// 最小全域木
            /// </summary>
            /// <returns></returns>
            public List<Edge> MinimumSpanningTree()
            {
                this.es.Sort();

                var uf = new UnionFind(V);
                var tree = new List<Edge>();
                foreach (var edge in this.es)
                {
                    if (!uf.Same(edge.v1, edge.v2))
                    {
                        uf.Merge(edge.v1, edge.v2);
                        tree.Add(edge);
                    }
                }

                return tree;
            }
        }

        #region Main
        public static void Main(string[] args)
        {
            // 出力が少ないときはこれをセットする方が時間かかるけれど
            // その場合は出力がボトルネックになっていないので、まあ良しとする
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 通常は引数無しで、ファイルから読み出したい場合はpath指定する
            var input = new Input();
            // 仮想的に標準入力をセットする
            //input.SetText("");

            Solve(input);

            Console.Out.Flush();
        }
        #endregion

        #region Competitive Template
#pragma warning disable CS0414
        static readonly int MOD = (int)1e9 + 7;
        static readonly int[] dx = { 1, 0, -1, 0 };
        static readonly int[] dy = { 0, 1, 0, -1 };
        /// <summary> 左上が原点 </summary>
        static readonly char[] dir = { 'R', 'U', 'L', 'D' };
#pragma warning restore CS0414

        /// <summary>
        /// 値を書き換え可能なTuple（ぐらいのイメージ）
        /// </summary>
        public class Pair<T, U> : IComparable<Pair<T, U>>, IEquatable<Pair<T, U>>
            where T : IComparable<T>
            where U : IComparable<U>
        {
            public T V1 { get; set; }
            public U V2 { get; set; }

            public Pair(T v1, U v2)
            {
                this.V1 = v1;
                this.V2 = v2;
            }

            public Pair(Pair<T, U> src) : this(src.V1, src.V2) { }

            public int CompareTo(Pair<T, U> other)
            {
                var r = this.V1.CompareTo(other.V1);
                if (r == 0) r = this.V2.CompareTo(other.V2);

                return r;
            }

            public bool Equals(Pair<T, U> other)
                => (other != null && this.CompareTo(other) == 0);

            public override string ToString()
                => $"{V1} {V2}";

            public override bool Equals(object obj)
                => Equals(obj as Pair<T, U>);

            public override int GetHashCode()
            {
                var hashCode = 1989511945;
                hashCode = hashCode * -1521134295 + V1.GetHashCode();
                hashCode = hashCode * -1521134295 + V2.GetHashCode();
                return hashCode;
            }

            public static bool operator ==(Pair<T, U> a, Pair<T, U> b)
            {
                if (object.ReferenceEquals(a, null))
                {
                    return object.ReferenceEquals(b, null);
                }

                return a.Equals(b);
            }

            public static bool operator !=(Pair<T, U> a, Pair<T, U> b)
                => !(a == b);
            public static bool operator <(Pair<T, U> a, Pair<T, U> b)
                => a.CompareTo(b) < 0;
            public static bool operator <=(Pair<T, U> a, Pair<T, U> b)
                => a.CompareTo(b) <= 0;
            public static bool operator >(Pair<T, U> a, Pair<T, U> b)
                => a.CompareTo(b) > 0;
            public static bool operator >=(Pair<T, U> a, Pair<T, U> b)
                => a.CompareTo(b) >= 0;
        }

        public class Input
        {
            // 変な改行コードがたまに混じっているので、一応セパレート指定する
            // スペース単独指定の方がもちろん早い
            static readonly char[] separator = { ' ', '\r', '\n' };
            readonly StreamReader sr;
            readonly Queue<string> queue;

            /// <summary>
            /// 特定のファイルから読み出したい場合はpath指定する
            /// </summary>
            public Input(string path = "")
            {
                queue = new Queue<string>();

                if (string.IsNullOrEmpty(path))
                {
                    sr = new StreamReader(Console.OpenStandardInput());
                }
                else
                {
                    sr = new StreamReader(path);
                }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                {
                    SetText(item);
                }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public bool SetText(string s)
            {
                if (s == null) return false;

                foreach (var elem in s.Trim()
                    .Split(separator, StringSplitOptions.RemoveEmptyEntries))
                {
                    queue.Enqueue(elem);
                }

                return true;
            }

            /// <summary>
            /// 要素が存在するか
            /// </summary>
            public bool Any()
                => queue.Any() || Read();

            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            bool Read()
            {
                if (!SetText(sr.ReadLine())) return false;

                if (!queue.Any()) return Read();

                return queue.Any();
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            public string Next()
            {
                if (!queue.Any() && !Read())
                {
                    return "";
                }

                return queue.Dequeue();
            }

            /// <summary>
            /// 指定個数queueにたまるまでenqueueし続ける
            /// </summary>
            bool Accumulate(int n)
            {
                while (queue.Count() < n)
                {
                    if (!Read()) return false;
                }

                return true;
            }

            public int NextInt() => int.Parse(Next());
            public long NextLong() => long.Parse(Next());
            public double NextDouble() => double.Parse(Next());

            /// <summary>
            /// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
            /// </summary>
            T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add)
            {
                if (!Accumulate(n)) return null;

                var a = new T[n];

                for (int i = 0; i < n; i++)
                    a[i] = add(parse(queue.Dequeue()), offset);

                return a;
            }

            public string[] Next(int n) => NextT(n, "", x => x, (x, y) => x);
            public int[] NextInt(int n, int offset = 0) => NextT(n, offset, int.Parse, (x, y) => x + y);
            public long[] NextLong(int n, long offset = 0) => NextT(n, offset, long.Parse, (x, y) => x + y);
            public double[] NextDouble(int n, double offset = 0.0) => NextT(n, offset, double.Parse, (x, y) => x + y);
        }

        static class Utils
        {
            public static void Print<T>(IEnumerable<T> ie, string sep = " ") => Console.WriteLine(string.Join(sep, ie));
            public static void Print<T>(params T[] objs) => Print(objs);
            public static T Max<T>(params T[] objs) => objs.Max();
            public static T Min<T>(params T[] objs) => objs.Min();

            public static void Swap<T>(ref T x, ref T y) where T : struct { var t = x; x = y; y = t; }
            public static void Swap<T>(IList<T> list, int i, int j) where T : struct { var t = list[i]; list[i] = list[j]; list[j] = t; }

            /// <summary>
            /// vでfillされたT[d1][d2]配列を作成する
            /// </summary>
            public static T[][] Create2DArray<T>(int d1, int d2, T v = default(T))
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(v, d2).ToArray()).ToArray();
            }

            /// <summary>
            /// vでfillされたT[d1][d2][d3]配列を作成する
            /// </summary>
            public static T[][][] Create3DArray<T>(int d1, int d2, int d3, T v = default(T))
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(0, d2).Select(__ =>
                    Enumerable.Repeat(v, d3).ToArray()).ToArray()).ToArray();
            }
        }
        #endregion
    }
}


Measured memory_bytes: 35352