Bonsai

No attempts yetTime limit1sMemory limit128 MB

Problem

Help chop every leaf off a bonsai tree.

You are given an undirected tree (a connected graph with no cycles) on nn vertices. Each edge (a branch) has a nonnegative integer weight (its thickness). One vertex rr is the root, and because the graph is a tree, every other vertex has a unique path to the root.

A leaf is a non-root vertex that has no children when the tree is rooted at rr — equivalently, a non-root vertex that is not the parent of any other vertex.

Determine the minimum total weight of edges that must be removed so that afterwards no leaf of the original tree is still connected to the root by any path.

Input

The input contains several test cases.

Each test case starts with a line holding two integers nn and rr (1n10001 \le n \le 1000, 1rn1 \le r \le n): the number of vertices and the index of the root.

The next n1n-1 lines each hold three integers uiu_i viv_i wiw_i (1ui,vin1 \le u_i, v_i \le n, 0wi10000 \le w_i \le 1000), meaning there is an undirected edge of weight wiw_i between vertices uiu_i and viv_i. No edge is listed twice, and the given edges always form a tree.

The input ends with a line containing 0 0, which is not a test case.

Output

For each test case, print a single line with one integer: the minimum total weight of edges that must be removed so that no original leaf is connected to the root.