The Rickshank Rickdemption

5

1 votes
Hard
Problem

Rick Sanchez is on a run from Galactic Federation. He is planning to install emergency exit portals on his planets in order to handle the emergency situation arising in case Galactic Militia try to catch him. Rick’s Galaxy is a collection of numerous interconnected planets. The planets are connected by strings in such a way that from any planet there is exactly one path to reach any other planet without visiting any intermediate planet (a planet that is on that path) more than once.

Rick’s portal gun runs on wiper fluid, which is very scarce. So, Rick does not want to waste it. In order to reduce usage of wiper fluid, Rick decided that not every planet will have an exit portal. Exit portal will be installed in such a way that if any planet does not have an exit portal then at least one of its adjacent planet must have one and for each string that connects two planets, at least one of the two planet must have an exit portal. Rick is busy, So he hired you to determine where to open the exit portals under this constraint.

However, as a first step, you are expected to determine the minimum number of exits required.

Input

The input file may contain multiple test cases. The first line of each test case contains an integer N (1 ≤ N ≤ 1, 000) indicating the number of planets in Rick’s galaxy, in this test case. Then follow N lines where the i-th (1 ≤ i ≤ N) line is the adjacency list of the i-th planet (Each planet is given a unique identification number from 1 to N for convenience). The adjacency list for planet i starts with an integer ni (1 ≤ ni ≤ N − 1) indicating the number of planets adjacent to this planet, followed by ni integers giving the identification numbers of those planets.

A test case containing a zero for N terminates the input.

Output

For each test case in the input file print a line containing the minimum number of exit portals required to meet the given constraint.

Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?