BUS - Bus
The city Buscelona (as the name suggests) has a great bus transport system. All buses have circular lines. The bus drivers in Buscelona like to chat. Fortunately most bus lines have some stops in common. If a bus driver meets a colleague on a bus stop they chat a bit and exchange all news they know.
The operation of buses is highly synchronized. The time necessary to get from one stop to the next stop is always exactly 1 minute.
Each morning each bus driver has some important news that only he knows. When a busdriver meets a colleague he will tell him all news he knows. If two bus drivers share the same start station, they will exchange their news there already (before they start working). Note that exchanging news and stopping does not take any time.
Input
The first line of a test case contains the number of bus lines n (0 < n < 50). The following n lines start with a number s (0 < s < 50) indicating the stops of a busline. On the same line follow s numbers representing a bus station each. A bus starts at the first station. When a bus reaches the last station, the bus will drive to the first station again.
There are many test cases separated by an empty line. Input data terminates with n = 0.
Output
For each test case you should output the time in minutes which it takes until all bus drivers know all news. If that never happens, your program should write the word "NEVER" (without quotes).
Example
Sample input: 3 3 1 2 3 3 2 3 1 4 2 3 4 5 2 2 1 2 2 5 8 0 Sample output: 12 NEVER
hide comments
Curiosa:
2010-07-13 22:28:54
how big could answer be? i've tried to solve this problem for about 225000 minutes and it says wrong answer |
Added by: | MichaĆ Czuczman |
Date: | 2004-07-03 |
Time limit: | 7s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All |
Resource: | Swiss Olympiad in Informatics 2004 |