Bounty Hunter II

No attempts yetTime limit2sMemory limit256 MB

Problem

Spike the bounty hunter is chasing another criminal across space. Hyperspace travel has made visiting several planets in a row much easier.

Every planet has a number of astral gates, and each gate is linked to a gate on another planet. For safety the link works in one direction only: one gate is the entry point into hyperspace and the other gate is the exit point. The network of hyperspace links also contains no cycle, a lesson from the gate accident of 2022 that destroyed most of the moon.

Looking at his star map, Spike wonders how many people he needs in order to search every planet. If two people visit the same planet the criminal grows suspicious and flees, so each planet must be visited by exactly one person. Each person may start at a planet of their choosing, and after that moves from planet to planet only along hyperspace links.

Find the minimum number of people needed to visit every planet.

Figure 1. The example inputs drawn as graphs.

Input

The first line contains the number of planets NN (0<N10000 < N \leq 1000). The planets are numbered 00 to N1N-1.

Each of the next NN lines describes the hyperspace links leaving one planet, the ii-th line those leaving planet ii. The line starts with the number of links KK (0KN10 \leq K \leq N-1), followed by the KK destination planets.

No sequence of links leads back to the planet it started from.

Output

Print the minimum number of people needed to visit every planet.