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 Go (1.6) (judge verdict: Accepted)

// Package main provides
//
// File:  d.go
// Author: ymiyamoto
//
// Created on Wed Jan 16 16:43:18 2019
//
package main

import (
	"container/heap"
	"fmt"
	"sort"
)

type point struct {
	index int
	x, y  int
}

func (p point) xLess(q point) bool {
	if p.x == q.x {
		return p.y < q.y
	}
	return p.x < q.x
}

func (p point) yLess(q point) bool {
	if p.y == q.y {
		return p.x < q.y
	}

	return p.y < q.y
}

type xpoints []point

func (xp xpoints) Len() int {
	return len(xp)
}

func (xp xpoints) Less(i, j int) bool {
	return xp[i].xLess(xp[j])
}

func (xp xpoints) Swap(i, j int) {
	xp[i], xp[j] = xp[j], xp[i]
}

type ypoints []point

func (yp ypoints) Len() int {
	return len(yp)
}

func (yp ypoints) Less(i, j int) bool {
	return yp[i].yLess(yp[j])
}

func (yp ypoints) Swap(i, j int) {
	yp[i], yp[j] = yp[j], yp[i]
}

type route struct {
	to, weight int
}

type item struct {
	r     route
	index int
}

type pQ []*item

func (pq pQ) Len() int {
	return len(pq)
}

func (pq pQ) Less(i, j int) bool {
	return pq[i].r.weight < pq[j].r.weight
}

func (pq pQ) Swap(i, j int) {
	pq[i].r, pq[j].r = pq[j].r, pq[i].r
	pq[i].index, pq[j].index = pq[j].index, pq[i].index
}

func (pq *pQ) Push(p interface{}) {
	x := p.(*item)
	x.index = len(*pq)
	*pq = append(*pq, x)
}

func (pq *pQ) Pop() interface{} {
	old := *pq
	n := len(old)
	i := old[n-1]
	*pq = old[0 : n-1]
	return i
}

func prim(g [][]route) int {
	visited := make([]bool, len(g))

	visited[0] = true
	pq := make(pQ, len(g[0]))
	for i := range g[0] {
		r := g[0][i]
		pq[i] = &item{r: r}
	}
	heap.Init(&pq)

	ans := 0
	for len(pq) > 0 {
		p := heap.Pop(&pq).(*item)
		if !visited[p.r.to] {
			ans += p.r.weight
			visited[p.r.to] = true
			for i := range g[p.r.to] {
				heap.Push(&pq, &item{r: g[p.r.to][i]})
			}
		}
	}

	return ans
}

func main() {
	var N int
	fmt.Scan(&N)

	xps := make(xpoints, N)
	yps := make(ypoints, N)
	for i := range xps {
		fmt.Scan(&xps[i].x, &xps[i].y)
		xps[i].index = i
		yps[i].index, yps[i].x, yps[i].y = xps[i].index, xps[i].x, xps[i].y
	}
	sort.Sort(xps)
	sort.Sort(yps)

	g := make([][]route, N)
	for i := range xps {
		if i+1 < len(xps) {
			d := xps[i+1].x - xps[i].x
			a, b := xps[i].index, xps[i+1].index
			g[a] = append(g[a], route{to: b, weight: d})
			g[b] = append(g[b], route{to: a, weight: d})
		}
		if i+1 < len(yps) {
			d := yps[i+1].y - yps[i].y
			a, b := yps[i].index, yps[i+1].index
			g[a] = append(g[a], route{to: b, weight: d})
			g[b] = append(g[b], route{to: a, weight: d})
		}
	}

	fmt.Println(prim(g))
}