Gnomes
Time limit1sMemory limit512 MB
Find the earliest hour when a token starting at station 1 can reach station n using swaps within unwatched stations each hour.
Statement
The wicked dragon Bitol invaded the land of the gnomes and enslaved its inhabitants. He assigned each of the gnomes a different workstation and then, sprawled on a heap of stolen treasure, lazily oversees their toil.
Because Bitol is an exceptionally slothful dragon, he does not watch all of his subjects at once. Instead he keeps a close eye only on the gnomes working at a certain group of stations at any given time. While he is looking away, all the gnomes he is not watching may meet one another and swap places freely (Bitol cannot remember which gnome worked at which station). Every hour the dragon turns his head and begins watching a different group of gnomes.
Bajtazyl, the gnome assigned station , wants to rally his fellow captives against Bitol. To do so he must first meet the venerable gnome Bajtomir, who was assigned station . Bajtazyl therefore faces a challenge: by swapping places with other gnomes at the right moments, he must, as quickly as possible, bring about a situation in which neither the station he is currently standing at nor station is being watched by the dragon.
Your task is to determine the earliest moment at which this meeting can take place. Fortunately, it is known that after hours Bitol will fall asleep, and from then on all the gnomes will be able to communicate freely.
Input
The first line of standard input contains two integers and (), the number of gnomes and the number of hours remaining until Bitol falls asleep. Each of the next lines describes the group of stations watched by the dragon during one hour. The description of the -th group consists of an integer (), the number of watched stations, followed by integers from in increasing order, the numbers of the watched stations. All numbers on a line are separated by single spaces.
You may assume that .
Output
Print a single integer from : the smallest number of hours after which Bajtazyl can reach Bajtomir.
Explanation
Explanation of the examples
In the first example, during the first hour of his journey Bajtazyl cannot leave station , because it is being watched by the dragon. During the second hour he can swap places with the gnome at station . Thanks to this, only at the start of the third hour does Bitol turn his head toward stations , , and , and Bajtazyl is finally able to meet Bajtomir.
In the second example, the meeting can happen immediately, because during the first hour the dragon is not looking at either Bajtazyl's or Bajtomir's station.
In the third example, the meeting can happen only once Bitol falls asleep.