Rigged Tournament

No attempts yetTime limit1sMemory limit128 MB

Problem

A tournament is a single-elimination competition among nn players. In each match two players compete: the loser is knocked out and the winner stays in (every match is decisive, there are no draws). The tournament ends when exactly one player is left, the champion.

The organizing federation fixes the schedule one match at a time. First it chooses the two players of match 1. Once that result is known, it chooses the two players (from those still in the tournament) of match 2, then match 3, and so on until a single player remains.

So the champion depends not only on the players' skill but also on the schedule, that is, on luck. The federation knows this. During the training season it watched the athletes closely, and for some pairs of players the result of a direct match is already certain.

Using that information, the federation asks, for a chosen player xx, whether it can arrange the schedule so that xx only ever meets opponents it is sure to beat (this of course makes xx win the whole tournament). If such a schedule exists, we say the tournament can be rigged for player xx.

Write a program that determines how many players the tournament can be rigged for.

Input

The first line contains an integer nn (1n10001 \le n \le 1000), the number of players. Players are numbered 1,2,,n1, 2, \ldots, n.

Each of the next nn lines describes one player. The ii-th of these lines lists the players who are certain to defeat player ii in a direct match: it starts with an integer mm (how many players are stronger than player ii) followed by their mm numbers. All numbers on a line are separated by single spaces.

  • Note 1: The fact that player aa would lose to bb and bb would lose to cc does not imply that aa would lose to cc in a direct match.
  • Note 2: The data never contains the contradictory situation where aa is on the list of players stronger than bb while bb is at the same time on the list of players stronger than aa.

Output

Output a single integer: the number of players for whom the tournament can be rigged.