from operator import itemgetter

from scipy.sparse.csgraph import minimum_spanning_tree

from scipy.sparse import csr_matrix



n = int(eval(input()))

xy = [list(map(int, input().split())) for _ in range(n)]



xyi = [[x, y, i] for i, (x, y) in enumerate(xy)]

edge = set()



xyi.sort()

for (x1, y1, i1), (x2, y2, i2) in zip(xyi, xyi[1:]):

    c = min(abs(x1 - x2), abs(y1 - y2))

    edge.add((i1, i2, c))



xyi.sort(key=itemgetter(1))

for (x1, y1, i1), (x2, y2, i2) in zip(xyi, xyi[1:]):

    c = min(abs(x1 - x2), abs(y1 - y2))

    edge.add((i1, i2, c))



row, col, cost = list(zip(*edge))



graph = csr_matrix((cost, (row, col)), shape=(n, n))

mst = minimum_spanning_tree(graph, overwrite=True).astype(int)

ans = mst.sum()

print(ans)

from operator import itemgetter

from collections import defaultdict

from scipy.sparse.csgraph import minimum_spanning_tree

from scipy.sparse import csr_matrix



n = int(eval(input()))

xy = [list(map(int, input().split())) + [i] for i in range(n)]

INF = 10 ** 9 + 1



d = defaultdict(lambda: INF)



for key in (itemgetter(0), itemgetter(1)):

    xy.sort(key=key)

    for (x1, y1, i1), (x2, y2, i2) in zip(xy, xy[1:]):

        c = min(abs(x1 - x2), abs(y1 - y2))

        d[(i1, i2)] = min(d[(i1, i2)], c)



row = []

col = []

cost = []

for (k1, k2), c in list(d.items()):

    row.append(k1)

    col.append(k2)

    cost.append(c)



graph = csr_matrix((cost, (row, col)), shape=(n, n))

mst = minimum_spanning_tree(graph, overwrite=True).astype(int)

ans = mst.sum()

print(ans)

from operator import itemgetter
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix

n = int(eval(input()))
xy = [list(map(int, input().split())) for _ in range(n)]
xyi = [[x, y, i] for i, (x, y) in enumerate(xy)]
edge = set()
xyi.sort()
for (x1, y1, i1), (x2, y2, i2) in zip(xyi, xyi[1:]):
    c = min(abs(x1 - x2), abs(y1 - y2))
    edge.add((i1, i2, c))
xyi.sort(key=itemgetter(1))
for (x1, y1, i1), (x2, y2, i2) in zip(xyi, xyi[1:]):
    c = min(abs(x1 - x2), abs(y1 - y2))
    edge.add((i1, i2, c))
row, col, cost = list(zip(*edge))
graph = csr_matrix((cost, (row, col)), shape=(n, n))
mst = minimum_spanning_tree(graph, overwrite=True).astype(int)
ans = mst.sum()
print(ans)

from operator import itemgetter
from collections import defaultdict
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix

n = int(eval(input()))
xy = [list(map(int, input().split())) + [i] for i in range(n)]
INF = 10**9 + 1
d = defaultdict(lambda: INF)
for key in (itemgetter(0), itemgetter(1)):
    xy.sort(key=key)
    for (x1, y1, i1), (x2, y2, i2) in zip(xy, xy[1:]):
        c = min(abs(x1 - x2), abs(y1 - y2))
        d[(i1, i2)] = min(d[(i1, i2)], c)
row = []
col = []
cost = []
for (k1, k2), c in list(d.items()):
    row.append(k1)
    col.append(k2)
    cost.append(c)
graph = csr_matrix((cost, (row, col)), shape=(n, n))
mst = minimum_spanning_tree(graph, overwrite=True).astype(int)
ans = mst.sum()
print(ans)

+from collections import defaultdict -xy = [list(map(int, input().split())) for _ in range(n)] -xyi = [[x, y, i] for i, (x, y) in enumerate(xy)] -edge = set() -xyi.sort() -for (x1, y1, i1), (x2, y2, i2) in zip(xyi, xyi[1:]): -    c = min(abs(x1 - x2), abs(y1 - y2)) -    edge.add((i1, i2, c)) -xyi.sort(key=itemgetter(1)) -for (x1, y1, i1), (x2, y2, i2) in zip(xyi, xyi[1:]): -    c = min(abs(x1 - x2), abs(y1 - y2)) -    edge.add((i1, i2, c)) -row, col, cost = list(zip(*edge)) +xy = [list(map(int, input().split())) + [i] for i in range(n)] +INF = 10**9 + 1 +d = defaultdict(lambda: INF) +for key in (itemgetter(0), itemgetter(1)): +    xy.sort(key=key) +    for (x1, y1, i1), (x2, y2, i2) in zip(xy, xy[1:]): +        c = min(abs(x1 - x2), abs(y1 - y2)) +        d[(i1, i2)] = min(d[(i1, i2)], c) +row = [] +col = [] +cost = [] +for (k1, k2), c in list(d.items()): +    row.append(k1) +    col.append(k2) +    cost.append(c)