COCI 2009/2010, Contest #7

Task SVEMIR

Fourth Great and Bountiful Human Empire is developing a transconduit tunnel network connecting all it's planets. The Empire consists of N planets, represented as points in the 3D space. The cost of forming a transconduit tunnel between planets A and B is:

TunnelCost[A,B] = min{ |xA - xB|, |yA - yB|, |zA - zB| }

where (xA, yA, zA) are 3D coordinates of planet A, and (xB, yB, zB) are coordinates of planet B. The Empire needs to build exactly N - 1 tunnels in order to fully connect all planets, either by direct links or by chain of links. You need to come up with the lowest possible cost of successfully completing this project.

Input

The first line of input contains one integer N (1 ≤ N ≤ 100 000), number of planets.

The next N lines contain exactly 3 integers each. All integers are between -109 and 109 inclusive. Each line contains the x, y, and z coordinate of one planet (in order).

No two planets will occupy the exact same point in space.

Output

The first and only line of output should contain the minimal cost of forming the network of tunnels.

Sample Tests

Input

2
1 5 10
7 8 2

Output

3

Input

3
-1 -1 -1
5 5 5
10 10 10

Output

11

Input

5
11 -15 -15
14 -5 -15
-1 -1 -5
10 -4 -1
19 -4 19

Output

4

All Submissions
Best Solutions


Point Value: 20 (partial)
Time Limit: 1.00s
Memory Limit: 32M
Added: Aug 13, 2013

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

It's quiet in here...