This winter is said to be the coldest in a hundred years. The city of Haebin wants to protect its park before the cold sets in, and the first job is draining the water left in the sprinklers.
Water drawn from the well enters the central node and runs along the pipes to neighboring nodes. Every node is either a sprinkler or an internal node that splits the water.
Shin Haebin, a city official, knows the sprinkler plumbing by heart and knows how much force each valve takes.
Haebin is lazy. He wants to shut a few valves and leave every sprinkler dry, spending as little force as he can.
How much force does he need?
The input holds several test cases and continues to the end of the file.
The first line of each test case has the number of nodes N (2≤N≤1000) and the number of the central node C (1≤C≤N).
Each of the next N−1 lines describes one pipe with three integers u, v (1≤u,v≤N) and w (1≤w≤1000). A pipe joins node u and node v, and shutting its valve takes force w.
The N−1 pipes form a tree that connects all N nodes.
For each test case print, on its own line, the smallest total force needed to shut valves so that no water reaches any sprinkler.