Central Tree

No attempts yetTime limit3sMemory limit128 MB

Problem

A tree is a connected graph with no cycles.

Call a vertex a central vertex if the sum of the (weighted) distances from it to every other vertex is as small as possible. When the number of vertices is small, you can find it easily by trying every vertex one by one.

For example, consider the following tree with 5 vertices. Name the vertices A,B,C,D,EA, B, C, D, E; the edges and their weights are:

  • BAB - A : 2
  • BCB - C : 1
  • BDB - D : 7
  • DED - E : 5

Here the central vertex is BB. The distances from BB to each vertex are BA=2,BC=1,BD=7,BE=7+5=12B \to A = 2,\quad B \to C = 1,\quad B \to D = 7,\quad B \to E = 7 + 5 = 12 so their sum is 2+1+7+12=222 + 1 + 7 + 12 = 22.

Write a program that, even when the number of vertices NN is large, reads a tree and computes the sum of the distances from every vertex to the central vertex (that is, the minimum possible sum defined above).

Input

The input consists of several test cases. The first line of each test case contains the number of vertices nn of the tree. (1n10,0001 \le n \le 10{,}000) The vertices are numbered from 00 to n1n-1.

Each of the following n1n-1 lines contains three integers aa, bb, and ww. (1w1001 \le w \le 100) This denotes an edge of weight ww connecting vertices aa and bb.

The last line of the input contains a single 00, marking the end of the input.

Output

For each test case, print on its own line the sum of the distances from every vertex to the central vertex (the minimum possible sum).