from collections import defaultdict

import operator



N = eval(input())

xs = []

ys = []

for i in range(N):

    x, y = list(map(int, input().split()))

    xs.append((x,i))

    ys.append((y,i))

xs.sort()

ys.sort()



es = defaultdict(lambda: float('inf'))

for i in range(N-1):

    xd = xs[i+1][0] - xs[i][0]

    i1 = min(xs[i+1][1], xs[i][1])

    i2 = max(xs[i+1][1], xs[i][1])

    es[(i1,i2)] = min(es[(i1,i2)], xd)

    yd = ys[i+1][0] - ys[i][0]

    i3 = min(ys[i+1][1], ys[i][1])

    i4 = max(ys[i+1][1], ys[i][1])

    es[(i3,i4)] = min(es[(i3,i4)], yd)



parent = [i for i in range(N)]

def root(a):

    if parent[a] == a:

        return a

    else:

        parent[a] = root(parent[a])

        return parent[a]

def is_same(a,b):

    return root(a) == root(b)

def unite(a,b):

    ra = root(a)

    rb = root(b)

    if ra == rb: return

    parent[ra] = rb;



value = 0

for k,v in sorted(list(es.items()), key=operator.itemgetter(1)):

    a,b = k

    if is_same(a,b): continue

    unite(a,b)

    value += v

print(value)
N = int(eval(input()))

src = [tuple(map(int,input().split())) for i in range(N)]



xs = []

ys = []

for i,(x,y) in enumerate(src):

    xs.append((x,i))

    ys.append((y,i))

xs.sort()

ys.sort()



es = []

for (x1,i1),(x2,i2) in zip(xs,xs[1:]):

    es.append((x2-x1, i1, i2))

for (y1,i1),(y2,i2) in zip(ys,ys[1:]):

    es.append((y2-y1, i1, i2))

es.sort()



class UnionFind:

    def __init__(self,N):

        self.parent = [i for i in range(N)]

        self.rank = [0] * N

        self.count = 0

    def root(self,a):

        if self.parent[a] == a:

            return a

        else:

            self.parent[a] = self.root(self.parent[a])

            return self.parent[a]

    def is_same(self,a,b):

        return self.root(a) == self.root(b)

    def unite(self,a,b):

        ra = self.root(a)

        rb = self.root(b)

        if ra == rb: return

        if self.rank[ra] < self.rank[rb]:

            self.parent[ra] = rb

        else:

            self.parent[rb] = ra

            if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1

        self.count += 1



ans = 0

uf = UnionFind(N)

for d,a,b in es:

    if uf.is_same(a,b): continue

    uf.unite(a,b)

    ans += d

print(ans)
from collections import defaultdict
import operator

N = eval(input())
xs = []
ys = []
for i in range(N):
    x, y = list(map(int, input().split()))
    xs.append((x, i))
    ys.append((y, i))
xs.sort()
ys.sort()
es = defaultdict(lambda: float("inf"))
for i in range(N - 1):
    xd = xs[i + 1][0] - xs[i][0]
    i1 = min(xs[i + 1][1], xs[i][1])
    i2 = max(xs[i + 1][1], xs[i][1])
    es[(i1, i2)] = min(es[(i1, i2)], xd)
    yd = ys[i + 1][0] - ys[i][0]
    i3 = min(ys[i + 1][1], ys[i][1])
    i4 = max(ys[i + 1][1], ys[i][1])
    es[(i3, i4)] = min(es[(i3, i4)], yd)
parent = [i for i in range(N)]


def root(a):
    if parent[a] == a:
        return a
    else:
        parent[a] = root(parent[a])
        return parent[a]


def is_same(a, b):
    return root(a) == root(b)


def unite(a, b):
    ra = root(a)
    rb = root(b)
    if ra == rb:
        return
    parent[ra] = rb


value = 0
for k, v in sorted(list(es.items()), key=operator.itemgetter(1)):
    a, b = k
    if is_same(a, b):
        continue
    unite(a, b)
    value += v
print(value)

N = int(eval(input()))
src = [tuple(map(int, input().split())) for i in range(N)]
xs = []
ys = []
for i, (x, y) in enumerate(src):
    xs.append((x, i))
    ys.append((y, i))
xs.sort()
ys.sort()
es = []
for (x1, i1), (x2, i2) in zip(xs, xs[1:]):
    es.append((x2 - x1, i1, i2))
for (y1, i1), (y2, i2) in zip(ys, ys[1:]):
    es.append((y2 - y1, i1, i2))
es.sort()


class UnionFind:
    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.rank = [0] * N
        self.count = 0

    def root(self, a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]

    def is_same(self, a, b):
        return self.root(a) == self.root(b)

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return
        if self.rank[ra] < self.rank[rb]:
            self.parent[ra] = rb
        else:
            self.parent[rb] = ra
            if self.rank[ra] == self.rank[rb]:
                self.rank[ra] += 1
        self.count += 1


ans = 0
uf = UnionFind(N)
for d, a, b in es:
    if uf.is_same(a, b):
        continue
    uf.unite(a, b)
    ans += d
print(ans)

-from collections import defaultdict -import operator - -N = eval(input()) +N = int(eval(input())) +src = [tuple(map(int, input().split())) for i in range(N)] -for i in range(N): -    x, y = list(map(int, input().split())) +for i, (x, y) in enumerate(src): -es = defaultdict(lambda: float("inf")) -for i in range(N - 1): -    xd = xs[i + 1][0] - xs[i][0] -    i1 = min(xs[i + 1][1], xs[i][1]) -    i2 = max(xs[i + 1][1], xs[i][1]) -    es[(i1, i2)] = min(es[(i1, i2)], xd) -    yd = ys[i + 1][0] - ys[i][0] -    i3 = min(ys[i + 1][1], ys[i][1]) -    i4 = max(ys[i + 1][1], ys[i][1]) -    es[(i3, i4)] = min(es[(i3, i4)], yd) -parent = [i for i in range(N)] +es = [] +for (x1, i1), (x2, i2) in zip(xs, xs[1:]): +    es.append((x2 - x1, i1, i2)) +for (y1, i1), (y2, i2) in zip(ys, ys[1:]): +    es.append((y2 - y1, i1, i2)) +es.sort() -def root(a): -    if parent[a] == a: -        return a -    else: -        parent[a] = root(parent[a]) -        return parent[a] +class UnionFind: +    def __init__(self, N): +        self.parent = [i for i in range(N)] +        self.rank = [0] * N +        self.count = 0 + +    def root(self, a): +        if self.parent[a] == a: +            return a +        else: +            self.parent[a] = self.root(self.parent[a]) +            return self.parent[a] + +    def is_same(self, a, b): +        return self.root(a) == self.root(b) + +    def unite(self, a, b): +        ra = self.root(a) +        rb = self.root(b) +        if ra == rb: +            return +        if self.rank[ra] < self.rank[rb]: +            self.parent[ra] = rb +        else: +            self.parent[rb] = ra +            if self.rank[ra] == self.rank[rb]: +                self.rank[ra] += 1 +        self.count += 1 -def is_same(a, b): -    return root(a) == root(b) - - -def unite(a, b): -    ra = root(a) -    rb = root(b) -    if ra == rb: -        return -    parent[ra] = rb - - -value = 0 -for k, v in sorted(list(es.items()), key=operator.itemgetter(1)): -    a, b = k -    if is_same(a, b): +ans = 0 +uf = UnionFind(N) +for d, a, b in es: +    if uf.is_same(a, b): -    unite(a, b) -    value += v -print(value) +    uf.unite(a, b) +    ans += d +print(ans)