from math import fabs


n = int(input())
volcanoes = []
for i in range(n):
	volcano = [int(x) for x in input().split()]
	volcanoes.append(volcano)

volcanoes.sort(reverse=True)

some_volcanoes = []
last_x = None
for x,y in volcanoes:
	if last_x == x:
		some_volcanoes[-1][1] = min(some_volcanoes[-1][1], y)
		some_volcanoes[-1][2] = max(some_volcanoes[-1][2], y)
	else:
		some_volcanoes.append([x,y,y])
		
upper_bonus = 0
upper_height = some_volcanoes[0][2]
bottom_bonus = 0
bottom_height = some_volcanoes[0][1]
last_x = some_volcanoes[0][0]

for i in range(1, len(some_volcanoes)):
	#print(f"some volcano: {some_volcanoes[i]}")
	new_x = some_volcanoes[i][0]
	new_upper_height = some_volcanoes[i][2]
	new_bottom_height = some_volcanoes[i][1]
	
	new_upper_bonus = min(upper_bonus + fabs(new_upper_height - upper_height), bottom_bonus + fabs(new_upper_height - bottom_height))
	#print(new_upper_bonus)
	#print(last_x, new_x)
	#print(new_upper_height, new_bottom_height)
	new_upper_bonus += (last_x - new_x) + (new_upper_height - new_bottom_height)
	new_bottom_bonus = min(upper_bonus + fabs(new_bottom_height - upper_height), bottom_bonus + fabs(new_bottom_height - bottom_height))
	new_bottom_bonus += (last_x - new_x) + (new_upper_height - new_bottom_height)
	
	#print(new_upper_bonus)
	#print(new_bottom_bonus)
	
	last_x = new_x
	upper_bonus = new_upper_bonus
	bottom_bonus = new_bottom_bonus
	upper_height = new_upper_height
	bottom_height = new_bottom_height
	
bonus = min(upper_bonus, bottom_bonus)

print(int(bonus))
		
