from heapq import *

N = int(eval(input()))

X,Y=[],[]

for n in range(N):

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

  X.append((x, n))

  Y.append((y, n))

X.sort()

Y.sort()

D=[[] for v in range(N)]

for n in range(N-1):

  cost = X[n+1][0]-X[n][0]

  D[X[n+1][1]].append((cost,X[n][1]))

  D[X[n][1]].append((cost,X[n+1][1]))

  cost=Y[n+1][0]-Y[n][0]

  D[Y[n+1][1]].append((cost,Y[n][1]))

  D[Y[n][1]].append((cost,Y[n+1][1]))



visited=[0]*N

pq = []

for w, t in D[0]:

  heappush(pq,(w,t))

visited[0]=1



ans = 0

while pq:

  w,t=heappop(pq)

  if visited[t]:

    continue

  visited[t]=1

  ans+=w

  for w,s in D[t]:

    if visited[s]==0:

      heappush(pq,(w,s))

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

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

sx = sorted(XYI, key=lambda x:x[0])

sy = sorted(XYI, key=lambda x:x[1])



es = []

for (x0,_,i0),(x1,_,i1) in zip(sx,sx[1:]):

  es.append((x1-x0, i0, i1))

for (_,y0,i0),(_,y1,i1) in zip(sy,sy[1:]):

  es.append((y1-y0, i0, i1))

es.sort(key=lambda x:x[0])

 

class UnionFind:

  def __init__(self,N):

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

    self._size = [1] * 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._size[ra] < self._size[rb]: ra,rb = rb,ra

    self._size[ra] += self._size[rb]

    self.parent[rb] = ra

    self.count += 1

  def size(self,a):

    return self._size[self.root(a)]



uf = UnionFind(N)

ans = 0

for d,a,b in es:

  if uf.is_same(a,b): continue

  uf.unite(a,b)

  ans += d

print(ans)
from heapq import *

N = int(eval(input()))
X, Y = [], []
for n in range(N):
    x, y = list(map(int, input().split()))
    X.append((x, n))
    Y.append((y, n))
X.sort()
Y.sort()
D = [[] for v in range(N)]
for n in range(N - 1):
    cost = X[n + 1][0] - X[n][0]
    D[X[n + 1][1]].append((cost, X[n][1]))
    D[X[n][1]].append((cost, X[n + 1][1]))
    cost = Y[n + 1][0] - Y[n][0]
    D[Y[n + 1][1]].append((cost, Y[n][1]))
    D[Y[n][1]].append((cost, Y[n + 1][1]))
visited = [0] * N
pq = []
for w, t in D[0]:
    heappush(pq, (w, t))
visited[0] = 1
ans = 0
while pq:
    w, t = heappop(pq)
    if visited[t]:
        continue
    visited[t] = 1
    ans += w
    for w, s in D[t]:
        if visited[s] == 0:
            heappush(pq, (w, s))
print(ans)

N = int(eval(input()))
XYI = [tuple(map(int, input().split() + [i])) for i in range(N)]
sx = sorted(XYI, key=lambda x: x[0])
sy = sorted(XYI, key=lambda x: x[1])
es = []
for (x0, _, i0), (x1, _, i1) in zip(sx, sx[1:]):
    es.append((x1 - x0, i0, i1))
for (_, y0, i0), (_, y1, i1) in zip(sy, sy[1:]):
    es.append((y1 - y0, i0, i1))
es.sort(key=lambda x: x[0])


class UnionFind:
    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self._size = [1] * 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._size[ra] < self._size[rb]:
            ra, rb = rb, ra
        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        self.count += 1

    def size(self, a):
        return self._size[self.root(a)]


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

-from heapq import * +N = int(eval(input())) +XYI = [tuple(map(int, input().split() + [i])) for i in range(N)] +sx = sorted(XYI, key=lambda x: x[0]) +sy = sorted(XYI, key=lambda x: x[1]) +es = [] +for (x0, _, i0), (x1, _, i1) in zip(sx, sx[1:]): +    es.append((x1 - x0, i0, i1)) +for (_, y0, i0), (_, y1, i1) in zip(sy, sy[1:]): +    es.append((y1 - y0, i0, i1)) +es.sort(key=lambda x: x[0]) -N = int(eval(input())) -X, Y = [], [] -for n in range(N): -    x, y = list(map(int, input().split())) -    X.append((x, n)) -    Y.append((y, n)) -X.sort() -Y.sort() -D = [[] for v in range(N)] -for n in range(N - 1): -    cost = X[n + 1][0] - X[n][0] -    D[X[n + 1][1]].append((cost, X[n][1])) -    D[X[n][1]].append((cost, X[n + 1][1])) -    cost = Y[n + 1][0] - Y[n][0] -    D[Y[n + 1][1]].append((cost, Y[n][1])) -    D[Y[n][1]].append((cost, Y[n + 1][1])) -visited = [0] * N -pq = [] -for w, t in D[0]: -    heappush(pq, (w, t)) -visited[0] = 1 + +class UnionFind: +    def __init__(self, N): +        self.parent = [i for i in range(N)] +        self._size = [1] * 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._size[ra] < self._size[rb]: +            ra, rb = rb, ra +        self._size[ra] += self._size[rb] +        self.parent[rb] = ra +        self.count += 1 + +    def size(self, a): +        return self._size[self.root(a)] + + +uf = UnionFind(N) -while pq: -    w, t = heappop(pq) -    if visited[t]: +for d, a, b in es: +    if uf.is_same(a, b): -    visited[t] = 1 -    ans += w -    for w, s in D[t]: -        if visited[s] == 0: -            heappush(pq, (w, s)) +    uf.unite(a, b) +    ans += d