Marbles on a tree

n boxes are placed on the vertices of a rooted tree, which are numbered from 1 to n, 1 ≤ n ≤ 10000. Each box is either empty or contains a number of marbles; the total number of marbles is n. The task is to move the marbles such that each box contains exactly one marble. This is to be accomplished be a sequence of moves; each move consists of moving one marble to a box at an adjacent vertex. What is the mini- mum number of moves required to achieve the goal? Input The input contains a number of cases. Each case starts with the number n followed by n lines. Each line contains at least three numbers which are: v the number of a vertex, followed by the number of marbles originally placed at vertex v followed by a number d which is the number of children of v, followed by d numbers giving the identities of the children of v. The input is terminated by a case where n = 0 and this case should not be processed. Output For each case in the input, output the smallest number of moves of marbles resulting in one marble at each vertex of the tree. Sample Input 9 123234 210 30256 413789 530 600 700 820 900 9 103234 200 30256 493789 500 600

2/2 700 800 900 9 103234 290 30256 403789 500 600 700 800 900 0 Sample Output 7 14 20