import numpy as np
from collections import defaultdict
import networkx as nx


def build_road_3():
	n = int(raw_input())
	cords = []
	for i in range(n):
		cords.append([int(e) for e in raw_input().split(" ")])
	cords = np.array(cords)
	x_cord = cords[:, 0]
	y_cord = cords[:, 1]
	# print x_cord
	sorted_indecies_x = np.argsort(x_cord)
	# print sorted_indecies_x
	sorted_indecies_y = np.argsort(y_cord)
	dict_weight_edges = defaultdict(list)
	for i in range(n - 1):
		s1 = sorted_indecies_x[i]
		d1 = sorted_indecies_x[i + 1]
		dis1 = abs(x_cord[s1] - x_cord[d1])
		dict_weight_edges[dis1].append((s1, d1))
		s2 = sorted_indecies_y[i]
		d2 = sorted_indecies_y[i + 1]
		dis2 = abs(y_cord[s2] - y_cord[d2])
		dict_weight_edges[dis2].append((s2, d2))
	sorted_keys = sorted(list(dict_weight_edges.keys()))
	G = nx.DiGraph()
	ans = 0
	for k in sorted_keys:
		if len(G.edges())==n-1:
			print ans
			return
		for e in dict_weight_edges.get(k):
			G.add_edge(*e)
			if list(nx.simple_cycles(G)):
				G.remove_edge(*e)
			else:
				ans = ans+k
	return


if __name__ == "__main__":
	build_road_3()