from operator import itemgetter

import heapq



N = int(eval(input()))

X = [[int(p) for p in input().split()] for _ in range(N)]



X.sort()

for i in range(N):

    X[i].append(i)

    

Y = sorted(X, key=itemgetter(1))



h = []

for i in range(N-1):

    heapq.heappush(h, (X[i+1][0]-X[i][0], i+1, i))

    heapq.heappush(h, (Y[i+1][1]-Y[i][1], Y[i+1][2], Y[i][2]))

    

    

class UnionFind():

    def __init__(self, n):

        self.n = n

        self.root = [-1]*(n+1)

        self.rnk = [0]*(n+1)

        

    def find_root(self, x):

        if self.root[x] < 0:

            return x

        else:

            self.root[x] = self.find_root(self.root[x])

            return self.root[x]

        

    def unite(self, x, y):

        x = self.find_root(x)

        y = self.find_root(y)

        if x == y:

            return

        elif self.rnk[x] > self.rnk[y]:

            self.root[x] += self.root[y]

            self.root[y] = x

        else:

            self.root[y] += self.root[x]

            self.root[x] = y

            if self.rnk[x] == self.rnk[y]:

                self.rnk[y] += 1

                

    def isSameGroup(self, x, y):

        return self.find_root(x) == self.find_root(y)

    

    def count(self, x):

        return -self.root[self.find_root(x)]

    

    

UF = UnionFind(N)

ans = 0

while h:

    t = heapq.heappop(h)

    if UF.isSameGroup(t[1], t[2]):

        continue

    ans += t[0]

    UF.unite(t[1], t[2])

    

print(ans)
from operator import itemgetter

import heapq

 

N = int(eval(input()))

pos = [[int(p) for p in input().split()] for _ in range(N)]

 

pos.sort()

for i in range(N):

    pos[i].append(i)

    

Y = sorted(pos, key=itemgetter(1))

Y_idx = [0]*N

for i in range(N):

    Y_idx[Y[i][2]] = i

    

ans = 0

used = [0]*N

used[0] = 1

h = []

c = 1

idx = Y_idx[0]

heapq.heappush(h, (pos[c][0]-pos[c-1][0], 1))

if idx > 0:

    heapq.heappush(h, (Y[idx][1]-Y[idx-1][1], Y[idx-1][2]))

if idx < N-1:

    heapq.heappush(h, (Y[idx+1][1]-Y[idx][1], Y[idx+1][2]))

    

cnt = 0

while cnt < N-1:

    t = heapq.heappop(h)

    if used[t[1]] == 1:

        continue

    c = t[1]

    used[c] = 1

    cnt += 1

    ans += t[0]

    if c > 0 and used[c-1] == 0:

        heapq.heappush(h, (pos[c][0]-pos[c-1][0], c-1))

    if c < N-1 and used[c+1] == 0:

        heapq.heappush(h, (pos[c+1][0]-pos[c][0], c+1))

    idx = Y_idx[c]

    if idx > 0 and used[Y[idx-1][2]] == 0:

        heapq.heappush(h, (Y[idx][1]-Y[idx-1][1], Y[idx-1][2]))

    if idx < N-1 and used[Y[idx+1][2]] == 0:

        heapq.heappush(h, (Y[idx+1][1]-Y[idx][1], Y[idx+1][2]))

            

print(ans)
from operator import itemgetter
import heapq

N = int(eval(input()))
X = [[int(p) for p in input().split()] for _ in range(N)]
X.sort()
for i in range(N):
    X[i].append(i)
Y = sorted(X, key=itemgetter(1))
h = []
for i in range(N - 1):
    heapq.heappush(h, (X[i + 1][0] - X[i][0], i + 1, i))
    heapq.heappush(h, (Y[i + 1][1] - Y[i][1], Y[i + 1][2], Y[i][2]))


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

    def find_root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find_root(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1

    def isSameGroup(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def count(self, x):
        return -self.root[self.find_root(x)]


UF = UnionFind(N)
ans = 0
while h:
    t = heapq.heappop(h)
    if UF.isSameGroup(t[1], t[2]):
        continue
    ans += t[0]
    UF.unite(t[1], t[2])
print(ans)

from operator import itemgetter
import heapq

N = int(eval(input()))
pos = [[int(p) for p in input().split()] for _ in range(N)]
pos.sort()
for i in range(N):
    pos[i].append(i)
Y = sorted(pos, key=itemgetter(1))
Y_idx = [0] * N
for i in range(N):
    Y_idx[Y[i][2]] = i
ans = 0
used = [0] * N
used[0] = 1
h = []
c = 1
idx = Y_idx[0]
heapq.heappush(h, (pos[c][0] - pos[c - 1][0], 1))
if idx > 0:
    heapq.heappush(h, (Y[idx][1] - Y[idx - 1][1], Y[idx - 1][2]))
if idx < N - 1:
    heapq.heappush(h, (Y[idx + 1][1] - Y[idx][1], Y[idx + 1][2]))
cnt = 0
while cnt < N - 1:
    t = heapq.heappop(h)
    if used[t[1]] == 1:
        continue
    c = t[1]
    used[c] = 1
    cnt += 1
    ans += t[0]
    if c > 0 and used[c - 1] == 0:
        heapq.heappush(h, (pos[c][0] - pos[c - 1][0], c - 1))
    if c < N - 1 and used[c + 1] == 0:
        heapq.heappush(h, (pos[c + 1][0] - pos[c][0], c + 1))
    idx = Y_idx[c]
    if idx > 0 and used[Y[idx - 1][2]] == 0:
        heapq.heappush(h, (Y[idx][1] - Y[idx - 1][1], Y[idx - 1][2]))
    if idx < N - 1 and used[Y[idx + 1][2]] == 0:
        heapq.heappush(h, (Y[idx + 1][1] - Y[idx][1], Y[idx + 1][2]))
print(ans)

-X = [[int(p) for p in input().split()] for _ in range(N)] -X.sort() +pos = [[int(p) for p in input().split()] for _ in range(N)] +pos.sort() -    X[i].append(i) -Y = sorted(X, key=itemgetter(1)) +    pos[i].append(i) +Y = sorted(pos, key=itemgetter(1)) +Y_idx = [0] * N +for i in range(N): +    Y_idx[Y[i][2]] = i +ans = 0 +used = [0] * N +used[0] = 1 -for i in range(N - 1): -    heapq.heappush(h, (X[i + 1][0] - X[i][0], i + 1, i)) -    heapq.heappush(h, (Y[i + 1][1] - Y[i][1], Y[i + 1][2], Y[i][2])) - - -class UnionFind: -    def __init__(self, n): -        self.n = n -        self.root = [-1] * (n + 1) -        self.rnk = [0] * (n + 1) - -    def find_root(self, x): -        if self.root[x] < 0: -            return x -        else: -            self.root[x] = self.find_root(self.root[x]) -            return self.root[x] - -    def unite(self, x, y): -        x = self.find_root(x) -        y = self.find_root(y) -        if x == y: -            return -        elif self.rnk[x] > self.rnk[y]: -            self.root[x] += self.root[y] -            self.root[y] = x -        else: -            self.root[y] += self.root[x] -            self.root[x] = y -            if self.rnk[x] == self.rnk[y]: -                self.rnk[y] += 1 - -    def isSameGroup(self, x, y): -        return self.find_root(x) == self.find_root(y) - -    def count(self, x): -        return -self.root[self.find_root(x)] - - -UF = UnionFind(N) -ans = 0 -while h: +c = 1 +idx = Y_idx[0] +heapq.heappush(h, (pos[c][0] - pos[c - 1][0], 1)) +if idx > 0: +    heapq.heappush(h, (Y[idx][1] - Y[idx - 1][1], Y[idx - 1][2])) +if idx < N - 1: +    heapq.heappush(h, (Y[idx + 1][1] - Y[idx][1], Y[idx + 1][2])) +cnt = 0 +while cnt < N - 1: -    if UF.isSameGroup(t[1], t[2]): +    if used[t[1]] == 1: +    c = t[1] +    used[c] = 1 +    cnt += 1 -    UF.unite(t[1], t[2]) +    if c > 0 and used[c - 1] == 0: +        heapq.heappush(h, (pos[c][0] - pos[c - 1][0], c - 1)) +    if c < N - 1 and used[c + 1] == 0: +        heapq.heappush(h, (pos[c + 1][0] - pos[c][0], c + 1)) +    idx = Y_idx[c] +    if idx > 0 and used[Y[idx - 1][2]] == 0: +        heapq.heappush(h, (Y[idx][1] - Y[idx - 1][1], Y[idx - 1][2])) +    if idx < N - 1 and used[Y[idx + 1][2]] == 0: +        heapq.heappush(h, (Y[idx + 1][1] - Y[idx][1], Y[idx + 1][2]))