using System;
using System.Collections.Generic;
using System.Linq;
using static Input;

public class Prog
{
    private const int INF = 1000000007;
    private const long INFINITY = 9223372036854775807;
    public struct Pair { public int x, y; public Pair(int xx, int yy) { x = xx; y = yy; } }
    private static int[] dx = { 1, 0, -1, 0 };
    private static int[] dy = { 0, -1, 0, 1 };
    public static void Main()
    {
        int N = NextInt();
        int[] x = new int[N];
        int[] y = new int[N];
        PriorityQueue<int, Pair> xs = new PriorityQueue<int, Pair>();
        PriorityQueue<int, Pair> ys = new PriorityQueue<int, Pair>();
        for (int i = 0; i < N; i++)
        {
            int xi = NextInt();
            int yi = NextInt();
            xs.Add(xi, new Pair(xi, i));
            ys.Add(yi, new Pair(yi, i));
        }

        PriorityQueue<int, int[]> e = new PriorityQueue<int, int[]>();
        //座標でソート、近い場所以外の道を建設するのはあうと
        //PQ(長さ,(都市A,都市B))
        for (int i = 1; i < N; i++)
        {
            var A = xs.Take();
            var B = ys.Take();
            var C = xs.Peek();
            var D = ys.Peek();
            e.Add(C.x - A.x, new int[3] { C.x - A.x, A.y, C.y });
            e.Add(D.x - B.x, new int[3] { D.x - B.x, B.y, D.y });
        }

        UnionFind UF = new UnionFind(N);
        long ans = 0;
        while (e.Count() != 0)
        {
            var data = e.Take();
            if (!UF.Same(data[1], data[2]))
            {
                UF.Unite(data[1], data[2]);
                ans += data[0];
            }
        }
        Console.WriteLine(ans);
    }
}


public class PriorityQueue<Tkey, Tvalue> where Tkey : IComparable
{
    private List<Tkey> keys = new List<Tkey>();
    private List<Tvalue> values = new List<Tvalue>();
    private int size = 0;
    private static bool Descending;
    private static int Compare(Tkey a, Tkey b) { return (Descending ? a.CompareTo(b) : b.CompareTo(a)); }

    public PriorityQueue(bool b = true) { Clear(); Descending = b; }
    public int Count() { return size; }
    public void Clear() { keys = new List<Tkey>(); values = new List<Tvalue>(); size = 0; }
    public void Add(Tkey e, Tvalue v)
    {
        keys.Add(e);
        values.Add(v);
        int x = size;
        size += 1;
        while (x != 0)
        {
            int par = (x % 2 == 0 ? x - 2 : x - 1) / 2;
            if (Compare(keys[par], e) <= 0) break;
            Tkey a = keys[x]; keys[x] = keys[par]; keys[par] = a;
            Tvalue b = values[x]; values[x] = values[par]; values[par] = b;
            x = par;
        }
    }
    public void Poll()
    {
        Tkey k = keys[size - 1];
        Tvalue v = values[size - 1];
        int x = 0;
        size--;
        if (size == 0) { Clear(); return; }
        keys.RemoveAt(size);
        values.RemoveAt(size);
        while (true)
        {
            int l = x * 2 + 1;
            int r = x * 2 + 2;
            if (size - 1 < l) break;
            int y = 0;
            if (size - 1 == l) { y = l; }
            else if (Compare(keys[l], keys[r]) == 1) { y = r; }
            else { y = l; }
            if (Compare(keys[y], k) > 0) break;
            keys[x] = keys[y];
            values[x] = values[y];
            x = y;
        }
        keys[x] = k;
        values[x] = v;
    }
    public Tvalue Take() { Tvalue ret = values[0]; Poll(); return ret; }
    public Tvalue Peek() { return values[0]; }
    public void Debug()
    {
        Console.Write("key   : ");
        foreach (var k in keys) Console.Write(k + " ");
        Console.WriteLine();
        Console.Write("value : ");
        foreach (var k in values) Console.Write(k + " ");
        Console.WriteLine();
    }
}

public class UnionFind
{
    public List<int> parent = new List<int>();

    public UnionFind(int N)
    {
        for (int i = 0; i < N; i++) parent.Add(i);
    }

    private int Root(int a)
    {
        if (parent[a] == a) return a;
        else return parent[a] = Root(parent[a]);
    }

    public void Unite(int a, int b)
    {
        a = Root(a);
        b = Root(b);
        if (a == b) return;
        parent[a] = parent[b];
    }

    public bool Same(int a, int b) { return (Root(a) == Root(b)); }
}

public class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static char NextChar() { Confirm(); return char.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}


Measured memory_bytes: 36764