package main

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

///////////////////////////////////////////////////

var V, E int
var edges []edge

func main() {
	log.SetFlags(log.Lshortfile)
	scn := newScanner()
	N := scn.nextInt()
	towns := make([]Town, N)
	for i := 0; i < N; i++ {
		a := scn.nextInt()
		b := scn.nextInt()
		towns[i].x = a
		towns[i].y = b
		towns[i].idx = i
	}
	// sort.Slice(towns, func(i, j int) bool {
	// 	return towns[i].x < towns[j].x
	// })
	sort.Sort(ByX(towns))
	log.Println(towns)
	for i := 0; i < N-1; i++ {
		a := towns[i].x
		b := towns[i+1].x
		c := towns[i].y
		d := towns[i+1].y
		cost := min(abs(a-b), abs(c-d))
		edges = append(edges, edge{u: towns[i].idx, v: towns[i+1].idx, cost: cost})
		edges = append(edges, edge{u: towns[i+1].idx, v: towns[i].idx, cost: cost})
	}
	// sort.Slice(towns, func(i, j int) bool {
	// 	return towns[i].y < towns[j].y
	// })
	sort.Sort(ByY(towns))
	log.Println(towns)
	for i := 0; i < N-1; i++ {
		a := towns[i].x
		b := towns[i+1].x
		c := towns[i].y
		d := towns[i+1].y
		cost := min(abs(a-b), abs(c-d))
		edges = append(edges, edge{u: towns[i].idx, v: towns[i+1].idx, cost: cost})
		edges = append(edges, edge{u: towns[i+1].idx, v: towns[i].idx, cost: cost})
	}
	V = N
	E = len(edges)
	fmt.Println(kruskal())
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

type Town struct {
	idx int
	x   int
	y   int
}

type ByX []Town

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

type ByY []Town

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

type edge struct {
	u, v, cost int
}

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 := 0; i < E; i++ {
		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)
}

///////////////////////////////////////////////////
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}

func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}

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

func newScanner() *scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 10000)
	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) nextInt64() int64 {
	v, _ := strconv.ParseInt(s.next(), 10, 64)
	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
		}
	}
}


Measured memory_bytes: 60928