The Task of Hennadiy
Following recent breakthroughs in teleportation technology, traveling to a city with coordinates (X_c, Y_c) from any location in the country with coordinates (X_c, y) or (x, Y_c) is now instantaneous for any x or y. Consequently, the distance between two cities located at (X_1, Y_1) and (X_2, Y_2) is determined by min(|X_1 - X_2|, |Y_1 - Y_2|).
Gennady, a traveling salesman, is planning a journey to visit N cities. He will begin his journey from the first city on his list. Each time he moves to a new city, he will choose the nearest unvisited city from his current location. If multiple cities are equidistant, he will prefer the one that appears first in his list.
Gennady has tasked you with writing a program to calculate the total distance he will travel based on his pre-determined list of cities.
Input
The first line contains an integer N (1 ≤ N ≤ 10^5), representing the number of cities on Gennady's list. The following N lines each contain a pair of integers X_i, Y_i (-10^5 ≤ X_i, Y_i ≤ 10^5), which are the coordinates of the cities on the list. Note that some cities may share the same coordinates.
Output
Output the total distance Gennady will travel.