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: Runtime Error)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

type point struct {
	x, y int
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func main() {
	solve()
}

type scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func newScanner() *scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &scanner{r: rdr}
}
func (s *scanner) next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *scanner) nextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *scanner) nextInt() int {
	v, _ := strconv.Atoi(s.next())
	return v
}

func (s *scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

var V int

func solve() {
	sc := newScanner()
	N := sc.nextInt()
	// fmt.Scan(&N)
	city := make([]point, N)
	for i := 0; i < N; i++ {
		city[i].x = sc.nextInt()
		city[i].y = sc.nextInt()
		// fmt.Scan(&city[i].x, &city[i].y)
	}
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {

			cost := min(abs(city[i].x-city[j].x), abs(city[i].y-city[j].y))
			if i == j {
				continue
			}
			// log.Println(i, j, cost)
			edges = append(edges, edge{u: i, v: j, cost: cost})
			// E++
		}
	}
	V = N
	// log.Println(edges)
	fmt.Println(kruskal())
}

type edge struct {
	u, v, cost int
}

var edges []edge

type ByCost []edge

func (a ByCost) Len() int           { return len(a) }
func (a ByCost) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByCost) Less(i, j int) bool { return a[i].cost < a[j].cost }

func kruskal() int {
	sort.Sort(ByCost(edges))
	uf := makeUnionFind(V)
	res := 0
	for i := range edges {
		e := edges[i]
		if !uf.Same(e.u, e.v) {
			uf.Unit(e.u, e.v)
			res += e.cost
		}
	}
	return res
}

type unionFind struct {
	par  []int
	size []int
	link [][]int
}

func makeUnionFind(count int) *unionFind {
	par := make([]int, count)
	size := make([]int, count)
	link := make([][]int, count)
	for i := range link {
		link[i] = make([]int, 1)
	}
	for i := 0; i < count; i++ {
		par[i] = i
		size[i] = 1
		link[i][0] = i
	}
	return &unionFind{par: par, size: size, link: link}
}

func (uf *unionFind) Find(a int) int {
	for uf.par[a] != a {
		uf.par[a] = uf.par[uf.par[a]]
		a = uf.par[a]
	}
	return a
}

func (uf *unionFind) Unit(a, b int) {
	a = uf.Find(a)
	b = uf.Find(b)
	if a != b {
		if uf.size[a] > uf.size[b] {
			a, b = b, a
		}
		uf.par[a] = b
		uf.size[b] += uf.size[a]
		uf.link[b] = append(uf.link[b], uf.link[a]...)
	}
}

func (uf *unionFind) Same(a, b int) bool {
	return uf.Find(a) == uf.Find(b)
}