import sys

sys.setrecursionlimit(10**9)

class UnionFind:

  def __init__(self,n):

    self.n=[-1]*n

    self.r=[0]*n

    self.siz=n

  def find_root(self,x):

    if self.n[x]<0:

      return x

    else:

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

      return self.n[x]

  def unite(self,x,y):

    x=self.find_root(x)

    y=self.find_root(y)

    if x==y:return

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

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

      self.n[y]=x

    else:

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

      self.n[x]=y

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

        self.r[y]+=1

    self.siz-=1

  def root_same(self,x,y):

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

  def count(self,x):

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

  def size(self):

    return self.siz



n=int(eval(input()))

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

G=[]



# MSTを求める

xy.sort()

for j in range(1,n):

  sx,sy,si=xy[j-1]

  tx,ty,ti=xy[j]

  G.append((min(abs(sx-tx),abs(sy-ty)),si,ti))



xy.sort(key=lambda x:x[1])

for j in range(1,n):

  sx,sy,si=xy[j-1]

  tx,ty,ti=xy[j]

  G.append((min(abs(sx-tx),abs(sy-ty)),si,ti))



# Kruskal 法

u=UnionFind(n)

ans=0

G.sort()

for c,x,y in G:

  if u.root_same(x,y):continue

  u.unite(x,y)

  ans+=c

print(ans)

import sys

sys.setrecursionlimit(10**7)





class UnionFind():

    def __init__(self, n):

        self.n = n

        self.parents = [-1] * n



    def find(self, x):

        if self.parents[x] < 0:

            return x

        else:

            self.parents[x] = self.find(self.parents[x])

            return self.parents[x]



    def union(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

        if self.parents[x] > self.parents[y]:

            x, y = y, x



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

        self.parents[y] = x



    def size(self, x):

        return -self.parents[self.find(x)]



    def same(self, x, y):

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



    def members(self, x):

        root = self.find(x)

        return [i for i in range(self.n) if self.find(i) == root]



    def roots(self):

        return [i for i, x in enumerate(self.parents) if x < 0]



    def group_count(self):

        return len(self.roots())



    def all_group_members(self):

        return {r: self.members(r) for r in self.roots()}



    def __str__(self):

        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())





n = int(eval(input()))

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

G = []



xy.sort()

for j in range(1, n):

    sx, sy, si = xy[j-1]

    tx, ty, ti = xy[j]

    G.append((min(abs(sx-tx), abs(sy-ty)), si, ti))



xy.sort(key=lambda x: x[1])

for j in range(1, n):

    sx, sy, si = xy[j-1]

    tx, ty, ti = xy[j]

    G.append((min(abs(sx-tx), abs(sy-ty)), si, ti))



uf = UnionFind(n)

ans = 0

G.sort()

for c, x, y in G:

    if uf.same(x, y):

        continue

    uf.union(x, y)

    ans += c

print(ans)

import sys

sys.setrecursionlimit(10**9)


class UnionFind:
    def __init__(self, n):
        self.n = [-1] * n
        self.r = [0] * n
        self.siz = n

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

    def unite(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        elif self.r[x] > self.r[y]:
            self.n[x] += self.n[y]
            self.n[y] = x
        else:
            self.n[y] += self.n[x]
            self.n[x] = y
            if self.r[x] == self.r[y]:
                self.r[y] += 1
        self.siz -= 1

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

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

    def size(self):
        return self.siz


n = int(eval(input()))
xy = [list(map(int, input().split())) + [i] for i in range(n)]
G = []
# MSTを求める
xy.sort()
for j in range(1, n):
    sx, sy, si = xy[j - 1]
    tx, ty, ti = xy[j]
    G.append((min(abs(sx - tx), abs(sy - ty)), si, ti))
xy.sort(key=lambda x: x[1])
for j in range(1, n):
    sx, sy, si = xy[j - 1]
    tx, ty, ti = xy[j]
    G.append((min(abs(sx - tx), abs(sy - ty)), si, ti))
# Kruskal 法
u = UnionFind(n)
ans = 0
G.sort()
for c, x, y in G:
    if u.root_same(x, y):
        continue
    u.unite(x, y)
    ans += c
print(ans)

import sys

sys.setrecursionlimit(10**7)


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())


n = int(eval(input()))
xy = [list(map(int, input().split())) + [i] for i in range(n)]
G = []
xy.sort()
for j in range(1, n):
    sx, sy, si = xy[j - 1]
    tx, ty, ti = xy[j]
    G.append((min(abs(sx - tx), abs(sy - ty)), si, ti))
xy.sort(key=lambda x: x[1])
for j in range(1, n):
    sx, sy, si = xy[j - 1]
    tx, ty, ti = xy[j]
    G.append((min(abs(sx - tx), abs(sy - ty)), si, ti))
uf = UnionFind(n)
ans = 0
G.sort()
for c, x, y in G:
    if uf.same(x, y):
        continue
    uf.union(x, y)
    ans += c
print(ans)

-sys.setrecursionlimit(10**9) +sys.setrecursionlimit(10**7) -        self.n = [-1] * n -        self.r = [0] * n -        self.siz = n +        self.n = n +        self.parents = [-1] * n -    def find_root(self, x): -        if self.n[x] < 0: +    def find(self, x): +        if self.parents[x] < 0: -            self.n[x] = self.find_root(self.n[x]) -            return self.n[x] +            self.parents[x] = self.find(self.parents[x]) +            return self.parents[x] -    def unite(self, x, y): -        x = self.find_root(x) -        y = self.find_root(y) +    def union(self, x, y): +        x = self.find(x) +        y = self.find(y) -        elif self.r[x] > self.r[y]: -            self.n[x] += self.n[y] -            self.n[y] = x -        else: -            self.n[y] += self.n[x] -            self.n[x] = y -            if self.r[x] == self.r[y]: -                self.r[y] += 1 -        self.siz -= 1 +        if self.parents[x] > self.parents[y]: +            x, y = y, x +        self.parents[x] += self.parents[y] +        self.parents[y] = x -    def root_same(self, x, y): -        return self.find_root(x) == self.find_root(y) +    def size(self, x): +        return -self.parents[self.find(x)] -    def count(self, x): -        return -self.n[self.find_root(x)] +    def same(self, x, y): +        return self.find(x) == self.find(y) -    def size(self): -        return self.siz +    def members(self, x): +        root = self.find(x) +        return [i for i in range(self.n) if self.find(i) == root] + +    def roots(self): +        return [i for i, x in enumerate(self.parents) if x < 0] + +    def group_count(self): +        return len(self.roots()) + +    def all_group_members(self): +        return {r: self.members(r) for r in self.roots()} + +    def __str__(self): +        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots()) -# MSTを求める -# Kruskal 法 -u = UnionFind(n) +uf = UnionFind(n) -    if u.root_same(x, y): +    if uf.same(x, y): -    u.unite(x, y) +    uf.union(x, y)