Problem:
Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

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

namespace UnionFind
{

    public class Sample
    {
        public class UnionFind{
            List<int> par;
            List<int> sizes;

            public UnionFind(int n){
                par = new List<int>();
                sizes = new List<int>();
                for(int i = 0; i < n; i++){
                    par.Add(i);
                    sizes.Add(1);
                }
            }

            public int Find(int x){
                if(x == par[x]) return x;
                return par[x] = Find(par[x]);
            }

            public void Unite(int x, int y){
                x = Find(x);
                y = Find(y);
                if(x == y) return;
                if(sizes[x] < sizes[y]){
                    int tmp = x;
                    x = y;
                    y = tmp;
                }
                par[y] = x;
                sizes[x] += sizes[y];
            }

            public bool Same(int x, int y){
                return Find(x) == Find(y);
            }

            public int size(int x){
                return sizes[Find(x)];
            }
        }

        public struct Edge
        {
            public int a, b, cost;
            public Edge(int a, int b, int cost){
                this.a = a;
                this.b = b;
                this.cost = cost;
            }
        }

        public class Graph
        {
            public Graph(){
                es = new List<Edge>();
            }

            public int n;
            public List<Edge> es;

            public int Kruskal()
            {
                es.Sort((a, b) => a.cost - b.cost);

                UnionFind uf = new UnionFind(n);
                int min_cost = 0;
                foreach(var e in es){
                    if(!uf.Same(e.a,e.b)){
                        min_cost += e.cost;
                        uf.Unite(e.a,e.b);
                    }
                }

                return min_cost;
            }
        }

        public static Graph Input_Graph(){
            Graph g = new Graph();
            
            int[] c = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
            g.n = c[0];
            int m = c[1];
            for(int i = 0; i < m; i++){
                int[] k = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
                g.es.Add(new Edge(k[0],k[1],k[2]));
            }
            return g;
        }

        public struct Node{
            public int i,x,y;
            public Node(int i, int x, int y){
                this.i = i;
                this.x = x;
                this.y = y;
            }
        }
        public static void Main(string[] args){
            // Graph g = Input_Graph();
            // Graph g = new Graph();
            // int n = Convert.ToInt32(Console.ReadLine());
            // g.n = n;
            // var nodes = new List<Node>();
            // for(int i = 0; i < n; i++){
            //     var c = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
            //     nodes.Add(new Node(i,c[0],c[1]));
            // }
            // nodes.Sort((a,b) => a.x.CompareTo(b.x));
            // for(int i = 0; i < n-1; i++){
            //     Edge e = new Edge(nodes[i].i, nodes[i+1].i,Math.Abs(nodes[i+1].x-nodes[i].x));
            //     g.es.Add(e);
            // }

            // nodes.Sort((a,b) => a.y.CompareTo(b.y));
            // for(int i = 0; i < n-1; i++){
            //     Edge e = new Edge(nodes[i].i, nodes[i+1].i,Math.Abs(nodes[i+1].y-nodes[i].y));
            //     g.es.Add(e);
            // }
            Graph g = new Graph();
            g.n = int.Parse(Console.ReadLine());
            var nodes = new List<Node>();
            for(int i = 0; i < g.n; i++){
                int[] k = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
                nodes.Add(new Node(i, k[0],k[1]));
            }
            nodes.Sort((a,b) => a.x - b.x);
            for(int i = 0; i < g.n - 1; i++){
                g.es.Add(new Edge(nodes[i].i, nodes[i+1].i,Math.Abs(nodes[i].x - nodes[i+1].x)));
            }
            nodes.Sort((a,b) => a.y - b.y);
            for(int i = 0; i < g.n - 1; i++){
                g.es.Add(new Edge(nodes[i].i, nodes[i+1].i,Math.Abs(nodes[i].y - nodes[i+1].y)));
            }
            Console.WriteLine(g.Kruskal());
        }
    }
}