The bus drivers of a small town are known to be real gossips. When two or more buses are at the same stop at the same time, the drivers quickly get out to exchange all the latest rumors they are aware of. This way, rumors spread like a wildfire throughout the city.

busroddels
Example of three bus routes: the red bus runs the loop route (1, 2, 3, 4, 5), the blue bus run the loop route (5, 6, 7, 8) and the green bus runs the loop route (3, 9, 6).

Each stop is labeled with a unique integer. As shown in the illustration above, each bus runs a loop route through the city. The route of a bus can thus be described by the sequence of integers that indicate the successive stops. After the last stop in the sequence, the bus revisits the first stop of the sequence. A bus can also frequent the same stop multiple times throughout a single loop, for example if the bus runs in an eight shaped route. The first stop in the sequence is the stop where the bus leaves in the morning. Drivers that leave at the same stop in the morning also exchange rumors at that stop. A bus having route (3, 6, 9) thus successively visits the stops 3 9 6 3 9 6 3 9 6 …. A bus having route (1, 2, 3, 2) successively visits the stops 1 2 3 2 1 2 3 2 1 2 3 2 ….

Assignment

In the morning, each bus driver has heard of one new rumor that is not known by the other bus drivers. Assume that the time between successive stops is 1 minute, and that exchanging rumors at a stop takes no time. After how many minutes are all bus drivers aware of all new rumors?

In order to answer this question, you write a function busgossip that takes a tuple describing the routes of all buses. Each route is itself described as a tuple containing integers (int) that indicate the labels of the successive stops in a single loop. The function must return an integer (int) that indicates the number of minutes it takes before all bus drivers are aware of all new rumors. If the drivers do not succeed at exchanging all rumors by the end of the day (i.e. after $$24 \times 60 = 1440$$ minutes), the function must return the value -1.

Example

>>> busgossip(((1, 2, 3, 4, 5), (5, 6, 7, 8), (3, 9, 6)))
12
>>> busgossip(((1, 2, 3), (2, 1, 3), (2, 4, 5, 3)))
4
>>> busgossip(((1, 2), (2, 1)))
-1