import numpy as np

from scipy.sparse import csr_matrix

from scipy.sparse.csgraph import minimum_spanning_tree

from operator import itemgetter

n=int(eval(input()))

lx=[]

ly=[]

XY = [[i] + [int(x) for x in input().split()] for i in range(n)]

edge = set()

for key in [itemgetter(1),itemgetter(2)]:

    XY.sort(key = key)

    for i in range(n-1):

        i1,x1,y1 = XY[i]

        i2,x2,y2 = XY[i+1]

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

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



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

value = np.array(value,dtype=int)

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



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

print((tree.sum()))

import numpy as np

from scipy.sparse import csr_matrix

from scipy.sparse.csgraph import minimum_spanning_tree

from operator import itemgetter

import sys

input = sys.stdin.readline

n=int(eval(input()))

lx=[]

ly=[]

XY = [[i] + [int(x) for x in input().split()] for i in range(n)]

edge = set()

for key in [itemgetter(1),itemgetter(2)]:

    XY.sort(key = key)

    for i in range(n-1):

        i1,x1,y1 = XY[i]

        i2,x2,y2 = XY[i+1]

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

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



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

value = np.array(value,dtype=int)

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



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

print((tree.sum()))

import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
from operator import itemgetter

n = int(eval(input()))
lx = []
ly = []
XY = [[i] + [int(x) for x in input().split()] for i in range(n)]
edge = set()
for key in [itemgetter(1), itemgetter(2)]:
    XY.sort(key=key)
    for i in range(n - 1):
        i1, x1, y1 = XY[i]
        i2, x2, y2 = XY[i + 1]
        d = min(abs(x1 - x2), abs(y1 - y2))
        edge.add((i1, i2, d))
row, col, value = list(zip(*edge))
value = np.array(value, dtype=int)
graph = csr_matrix((value, (row, col)), shape=(n, n))
tree = minimum_spanning_tree(graph, overwrite=True).astype(int)
print((tree.sum()))

import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
from operator import itemgetter
import sys

input = sys.stdin.readline
n = int(eval(input()))
lx = []
ly = []
XY = [[i] + [int(x) for x in input().split()] for i in range(n)]
edge = set()
for key in [itemgetter(1), itemgetter(2)]:
    XY.sort(key=key)
    for i in range(n - 1):
        i1, x1, y1 = XY[i]
        i2, x2, y2 = XY[i + 1]
        d = min(abs(x1 - x2), abs(y1 - y2))
        edge.add((i1, i2, d))
row, col, value = list(zip(*edge))
value = np.array(value, dtype=int)
graph = csr_matrix((value, (row, col)), shape=(n, n))
tree = minimum_spanning_tree(graph, overwrite=True).astype(int)
print((tree.sum()))

+import sys +input = sys.stdin.readline