package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

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

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

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

// Main
const MOD = int(1e+9) + 7
const INF = 1 << 60

type UnionFind struct {
	parent []int
}

func NewUnionFind(n int) *UnionFind {
	p := make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = i
	}
	return &UnionFind{p}
}

func (a *UnionFind) root(x int) int {
	if a.parent[x] == x {
		return x
	} else {
		a.parent[x] = a.root(a.parent[x])
		return a.parent[x]
	}
}

func (a *UnionFind) isSame(x, y int) bool {
	return a.root(x) == a.root(y)
}

func (a *UnionFind) unite(x, y int) {
	x = a.root(x)
	y = a.root(y)
	if x == y {
		return
	} else {
		a.parent[x] = y
	}
}

type Town struct {
	id, x, y int
}

type ByX []Town

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

type ByY []Town

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

type Edge struct {
	from, to, cost int
}

type ByCost []Edge

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

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	T := make([]Town, N)
	for i := 0; i < N; i++ {
		T[i] = Town{i, sc.nextInt(), sc.nextInt()}
	}
	var E []Edge
	sort.Sort(ByX(T))
	for i := 0; i < N-1; i++ {
		from, to := T[i], T[i+1]
		E = append(E, Edge{from.id, to.id, f(from.x, to.x, from.y, to.y)})
	}
	sort.Sort(ByY(T))
	for i := 0; i < N-1; i++ {
		from, to := T[i], T[i+1]
		E = append(E, Edge{from.id, to.id, f(from.x, to.x, from.y, to.y)})
	}
	sort.Sort(ByCost(E))
	ans := 0
	un := NewUnionFind(N)
	for _, e := range E {
		if !un.isSame(e.from, e.to) {
			un.unite(e.from, e.to)
			ans += e.cost
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

func f(x, x2, y, y2 int) int {
	return min(abs(x-x2), abs(y-y2))
}


Measured memory_bytes: 27392