length = int(input())
target = []
for i in range(length):
    a,b = (int(n) for n in input().split(" "))
    target.append([a,b,i])
target.sort()
bef_x,bef_y = -1,-1
for i in range(length-1,-1,-1):
    if bef_x == target[i][0] and bef_y == target[i][1]:
        target.pop(i)
        length -= 1
    else:
        bef_x = target[i][0]
        bef_y = target[i][1]

x_sorted = target[:]
y_sorted = (sorted(target,key=lambda x:x[1]))[:]
count = 0
already = set()
now_x = 0
now_y = 0
now_rev_x = length - 1
now_rev_y = length - 1
answer = 0
# print(x_sorted)
# print(y_sorted)
while count < length - 1:

    x_diff = abs(x_sorted[now_x][0] - x_sorted[now_x + 1][0])
    rev_x_diff = abs(x_sorted[now_rev_x][0] - x_sorted[now_rev_x - 1][0])
    y_diff = abs(y_sorted[now_y][1] - y_sorted[now_y + 1][1])
    rev_y_diff = abs(y_sorted[now_rev_y][1] - y_sorted[now_rev_y - 1][1])

    min_choice = min(x_diff,rev_x_diff,y_diff,rev_y_diff)

    if min_choice == x_diff:
        if ({now_x,now_x - 1} - already):
            answer += min_choice
            now_x += 1
            already |= {now_x,now_x - 1}
        else:
            now_x += 1
    elif min_choice == y_diff:
        if ({now_y,now_y + 1} - already):
            answer +=  min_choice
            already |= {now_y,now_y + 1}
            now_y += 1
        else:
            now_y += 1

    elif min_choice == rev_x_diff:
        if ({now_rev_x,now_rev_x - 1} - already):
            answer += min_choice
            already |= {now_rev_x,now_rev_x - 1}
            now_rev_x -= 1
        else:
            now_rev_x -= 1
    else:
        if ({now_rev_y,now_rev_y - 1} - already):
            answer +=  min_choice
            already |= {now_rev_y,now_rev_y - 1}
            now_rev_y -= 1
        else:
            now_rev_y -= 1
    if now_x == now_rev_x:
        x_sorted[now_x] = float("inf")
    elif now_y == now_rev_y:
        y_sorted[now_y] = float("inf")
    # print(x_diff,rev_x_diff,y_diff,rev_y_diff,now_x,now_rev_x,now_y,now_rev_y,answer,already)
    count += 1
print(answer)
