import numpy as np

def build_road():
	n = int(raw_input())
	cords = []
	for i in range(n):
		cords.append([int(e) for e in raw_input().split(" ")])
	# print cords
	mutual_distance = np.zeros((n, n))
	# print mutual_distance
	for i in range(n):
		for j in range(n):
			mutual_distance[i][j] = \
				min(abs(cords[i][0]-cords[j][0]),
					abs(cords[i][1]-cords[j][1]))
	from scipy.sparse.csgraph import minimum_spanning_tree
	ans = minimum_spanning_tree(mutual_distance)
	ans = ans.toarray().astype(int)
	print ans.sum()


if __name__ == "__main__":
	build_road()