from scipy.sparse.csgraph import minimum_spanning_tree

from scipy.sparse import csr_matrix

from operator import itemgetter



n = int(eval(input()))

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



edge = set()

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

    xy.sort(key=key)

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

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

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



u, v, c = list(zip(*edge))



graph = csr_matrix((c, (u, v)), shape=(n, n), dtype=int)

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

ans = mst.sum()

print(ans)

from scipy.sparse.csgraph import minimum_spanning_tree

from scipy.sparse import csr_matrix

from operator import itemgetter



n = int(eval(input()))

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



edge = set()

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

    xy.sort(key=key)

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

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

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



u, v, c = list(zip(*edge))



graph = csr_matrix((c, (u, v)), shape=(n, n))

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

ans = mst.sum()

print(ans)

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

n = int(eval(input()))
xy = [list(map(int, input().split())) + [i] for i in range(n)]
edge = set()
for key in (itemgetter(0), itemgetter(1)):
    xy.sort(key=key)
    for (x1, y1, i1), (x2, y2, i2) in zip(xy, xy[1:]):
        d = min(abs(x1 - x2), abs(y1 - y2))
        edge.add((i1, i2, d))
u, v, c = list(zip(*edge))
graph = csr_matrix((c, (u, v)), shape=(n, n), dtype=int)
mst = minimum_spanning_tree(graph, overwrite=True).astype(int)
ans = mst.sum()
print(ans)

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

n = int(eval(input()))
xy = [list(map(int, input().split())) + [i] for i in range(n)]
edge = set()
for key in (itemgetter(0), itemgetter(1)):
    xy.sort(key=key)
    for (x1, y1, i1), (x2, y2, i2) in zip(xy, xy[1:]):
        d = min(abs(x1 - x2), abs(y1 - y2))
        edge.add((i1, i2, d))
u, v, c = list(zip(*edge))
graph = csr_matrix((c, (u, v)), shape=(n, n))
mst = minimum_spanning_tree(graph, overwrite=True).astype(int)
ans = mst.sum()
print(ans)

-graph = csr_matrix((c, (u, v)), shape=(n, n), dtype=int) +graph = csr_matrix((c, (u, v)), shape=(n, n))