from heapq import *



# Aの中でxの根を求める

def find(A,x):

    p = A[x]

    if p == x:

        return x

    a = find(A,p)

    A[x] = a

    return a



# Aのリストでxとyの属する集合の併合

def union(A, x, y):

    if find(A,x) > find(A,y):

        bx, by = find(A,y), find(A,x)

    else:

        bx, by = find(A,x), find(A,y)

    # 根をbxに統一

    A[by] = bx



N = int(eval(input()))

X = [(0,0)] * N

Y = [(0,0)] * N

E = [(0,0)] * N



for i in range(N):

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

    X[i] = (x, i)

    Y[i] = (y, i)

    E[i] = (x, y)



X.sort()

Y.sort()

H = []

heapify(H)



for i in range(N - 1):

    x1, j1 = X[i]

    x2, j2 = X[i + 1]

    y1, k1 = Y[i]

    y2, k2 = Y[i + 1]

    # ヒープ構造を用いて常にHの最初に最小値を持ってくる

    # X座標とY座標でそれぞれHに追加

    heappush(H, (x2 - x1, j1, j2))

    heappush(H, (y2 - y1, k1, k2))



answer = 0

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



while H:

    w, s, t = heappop(H)

    # sとtの集合が異なっていた時、道を作る

    if find(V, s) != find(V, t):

        union(V, s, t)

        answer += w



print(answer)

# D - Built?

from heapq import *



# Aでxの根を求める

def find(A, x):

    p = A[x]

    if p == x:

        return x

    a = find(A, p)

    A[x] = a

    return a



# Aでxとyの属する集合の併合

def union(A, x, y):

    if find(A, x) > find(A, y):

        bx, by = find(A, y), find(A, x)

    else:

        bx, by = find(A, x), find(A, y)

    # 根をbx(値の小さい方)に統一 

    A[by] = bx



N = int(eval(input()))

X = [(0,0)] * N

Y = [(0,0)] * N



for i in range(N):

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

    X[i] = (x, i)

    Y[i] = (y, i)



X.sort()

Y.sort()

H = []

heapify(H)



for i in range(N - 1):

    x1, j1 = X[i]

    x2, j2 = X[i+1]

    y1, k1 = Y[i]

    y2, k2 = Y[i+1]

    # ヒープ構造を用いて常にHの先頭に最小値を持ってくる

    # X座標とY座標でそれぞれHに追加

    heappush(H, (x2 - x1, j1, j2))

    heappush(H, (y2 - y1, k1, k2))



answer = 0

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



while H:

    w, s, t = heappop(H)

    # sとtの集合が異なっていた時、道を作る

    if find(V, s) != find(V, t):

        union(V, s, t)

        answer += w



print(answer)
from heapq import *

# Aの中でxの根を求める
def find(A, x):
    p = A[x]
    if p == x:
        return x
    a = find(A, p)
    A[x] = a
    return a


# Aのリストでxとyの属する集合の併合
def union(A, x, y):
    if find(A, x) > find(A, y):
        bx, by = find(A, y), find(A, x)
    else:
        bx, by = find(A, x), find(A, y)
    # 根をbxに統一
    A[by] = bx


N = int(eval(input()))
X = [(0, 0)] * N
Y = [(0, 0)] * N
E = [(0, 0)] * N
for i in range(N):
    x, y = list(map(int, input().split()))
    X[i] = (x, i)
    Y[i] = (y, i)
    E[i] = (x, y)
X.sort()
Y.sort()
H = []
heapify(H)
for i in range(N - 1):
    x1, j1 = X[i]
    x2, j2 = X[i + 1]
    y1, k1 = Y[i]
    y2, k2 = Y[i + 1]
    # ヒープ構造を用いて常にHの最初に最小値を持ってくる
    # X座標とY座標でそれぞれHに追加
    heappush(H, (x2 - x1, j1, j2))
    heappush(H, (y2 - y1, k1, k2))
answer = 0
V = [i for i in range(N)]
while H:
    w, s, t = heappop(H)
    # sとtの集合が異なっていた時、道を作る
    if find(V, s) != find(V, t):
        union(V, s, t)
        answer += w
print(answer)

# D - Built?
from heapq import *

# Aでxの根を求める
def find(A, x):
    p = A[x]
    if p == x:
        return x
    a = find(A, p)
    A[x] = a
    return a


# Aでxとyの属する集合の併合
def union(A, x, y):
    if find(A, x) > find(A, y):
        bx, by = find(A, y), find(A, x)
    else:
        bx, by = find(A, x), find(A, y)
    # 根をbx(値の小さい方)に統一
    A[by] = bx


N = int(eval(input()))
X = [(0, 0)] * N
Y = [(0, 0)] * N
for i in range(N):
    x, y = list(map(int, input().split()))
    X[i] = (x, i)
    Y[i] = (y, i)
X.sort()
Y.sort()
H = []
heapify(H)
for i in range(N - 1):
    x1, j1 = X[i]
    x2, j2 = X[i + 1]
    y1, k1 = Y[i]
    y2, k2 = Y[i + 1]
    # ヒープ構造を用いて常にHの先頭に最小値を持ってくる
    # X座標とY座標でそれぞれHに追加
    heappush(H, (x2 - x1, j1, j2))
    heappush(H, (y2 - y1, k1, k2))
answer = 0
V = [i for i in range(N)]
while H:
    w, s, t = heappop(H)
    # sとtの集合が異なっていた時、道を作る
    if find(V, s) != find(V, t):
        union(V, s, t)
        answer += w
print(answer)

+# D - Built? -# Aの中でxの根を求める +# Aでxの根を求める -# Aのリストでxとyの属する集合の併合 +# Aでxとyの属する集合の併合 -    # 根をbxに統一 +    # 根をbx(値の小さい方)に統一 -E = [(0, 0)] * N -    E[i] = (x, y) -    # ヒープ構造を用いて常にHの最初に最小値を持ってくる +    # ヒープ構造を用いて常にHの先頭に最小値を持ってくる