You are given $$$n$$$ houses in a town located at coordinates $$$(x_i, y_i)$$$ on a 2D grid, where $$$1 \leq i \leq n$$$.
The town council wants to connect all houses with roads using square tiles. Each tile covers exactly one unit of distance and can only be placed horizontally (parallel to x-axis) or vertically (parallel to y-axis).
To connect two houses, you need to place tiles to form a path between them. The shortest path will use the minimum number of horizontal and vertical tiles needed to travel from one house coordinate to another.
Find the minimum total number of tiles required to build roads that connect all houses such that there exists exactly one path between any two houses.
The first line contains an integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$ — the number of houses. The next $$$n$$$ lines each contain two integers $$$x_i, y_i$$$ $$$(-10^6 \leq x_i, y_i \leq 10^6)$$$ — the coordinates of the $$$i$$$-th house. All house locations are distinct.
Output a single integer — the minimum number of tiles needed to connect all houses.
Input:
5 0 0 2 2 3 10 5 2 7 0
Output:
20
Input:
3 -4 1 -2 5 3 12
Output:
18
In the first sample, we have 5 houses at coordinates: $$$(0,0)$$$, $$$(2,2)$$$, $$$(3,10)$$$, $$$(5,2)$$$, and $$$(7,0)$$$.
One optimal way to connect all houses with minimum tiles:
Total tiles required: $$$4 + 3 + 4 + 9 = 20$$$.
In the second sample, we have 3 houses at coordinates: $$$(-4,1)$$$, $$$(-2,5)$$$, and $$$(3,12)$$$.
All houses can be connected with minimum cost of $$$18$$$ tiles. The optimal road network forms a path: $$$(-4,1) \to (-2,5) \to (3,12)$$$.