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

import (
	"bufio"
	"container/heap"
	"fmt"
	"io"
	"os"
	"sort"
	"strconv"
)

var (
	readString func() string
	stdout     *bufio.Writer
)

func init() {
	readString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readInt()
	}
	return b
}

func readLengthAndSlice() []int {
	n := readInt()
	return readIntSlice(n)
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minUint(a, b uint) uint {
	if a < b {
		return a
	}
	return b
}

func minInt64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minUint64(a, b uint64) uint64 {
	if a < b {
		return a
	}
	return b
}

func maxInt(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxUint(a, b uint) uint {
	if a < b {
		return b
	}
	return a
}
func maxInt64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxUint64(a, b uint64) uint64 {
	if a < b {
		return b
	}
	return a
}

func absInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absInt64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// readString() string
// readInt() int
// readIntSlice(n int) []int
// readLengthAndSlice() []int
type town struct {
	id   int
	x, y int
}
type townSliceX []town

func (ts townSliceX) Len() int {
	return len(ts)
}
func (ts townSliceX) Less(i, j int) bool {
	return ts[i].x < ts[j].x
}
func (ts townSliceX) Swap(i, j int) {
	ts[i], ts[j] = ts[j], ts[i]
}

type townSliceY []town

func (ts townSliceY) Len() int {
	return len(ts)
}
func (ts townSliceY) Less(i, j int) bool {
	return ts[i].y < ts[j].y
}
func (ts townSliceY) Swap(i, j int) {
	ts[i], ts[j] = ts[j], ts[i]
}

type cost struct {
	val      int
	from, to int
}

type costSlice []cost

func (cs costSlice) Len() int {
	return len(cs)
}
func (cs costSlice) Less(i, j int) bool {
	return cs[i].val < cs[j].val
}
func (cs costSlice) Swap(i, j int) {
	cs[i], cs[j] = cs[j], cs[i]
}
func (cs *costSlice) Push(x interface{}) {
	item := x.(cost)
	*cs = append(*cs, item)
}
func (cs *costSlice) Pop() interface{} {
	c := *cs
	n := len(c) - 1
	if n == -1 {
		return nil
	}
	x := c[n]
	*cs = c[:n]
	return x
}

func main() {
	defer stdout.Flush()

	n := readInt()
	ts := make([]town, n)
	for i := 0; i < n; i++ {
		ts[i].id = i
		ts[i].x = readInt()
		ts[i].y = readInt()
	}

	E := make([][]cost, n)

	sort.Sort(townSliceX(ts))
	for i := 0; i+1 < n; i++ {
		a := ts[i].id
		b := ts[i+1].id
		c := absInt(ts[i].x - ts[i+1].x)
		E[a] = append(E[a], cost{c, a, b})
		E[b] = append(E[b], cost{c, b, a})
	}

	sort.Sort(townSliceY(ts))
	for i := 0; i+1 < n; i++ {
		a := ts[i].id
		b := ts[i+1].id
		c := absInt(ts[i].y - ts[i+1].y)
		E[a] = append(E[a], cost{c, a, b})
		E[b] = append(E[b], cost{c, b, a})
	}

	v := make(map[int]bool)

	h := new(costSlice)
	heap.Init(h)
	pop := func() (cost, bool) {
		x := heap.Pop(h)
		if x == nil {
			return cost{}, false
		}
		return x.(cost), true
	}

	push := func(c cost) {
		heap.Push(h, c)
	}

	v[0] = true
	for _, c := range E[0] {
		push(c)
	}

	var ans int64
	for len(v) != n {
		e, ok := pop()
		if !ok {
			panic("no edges found")
		}
		if v[e.to] {
			continue
		}
		ans += int64(e.val)
		v[e.to] = true
		for _, c := range E[e.to] {
			push(c)
		}
	}
	println(ans)
}