Under construction

Still being built. Things may change

Spinning Wheels

Interview

Time limit1sMemory limit1024 MB

Summary
Find the earliest integer second when wedges on all five spinning wheels line up so a beam passes through, or report none.
Level

Easy3 of 10

Topics
Brute force, Simulation, Implementation
Solved
No attempts yet

Problem

Five opaque spinning wheels each have one or more wedges cut out of their edges. These wedges must be aligned quickly and correctly to solve this problem. Each wheel has an alignment mark at 0 degrees, so all the wheels can start in a known position. Wheels rotate in the 'plus degrees' direction, so shortly after they start, they pass through 1 degree, 2 degrees, and so on, though probably not at the same time.

This is an integer problem. Wheels are never at 1.5 degrees or 23.51234123 degrees. A wheel can move from 20 to 25 degrees in a single second, or from 30 to 40 degrees if it spins quickly. This removes any need to handle fractional seconds.

All angles are integers in the range 0≤angle≤3590 \le angle \le 359. The angle 0 comes right after 359.

Each wheel turns at 1≤speed≤1801 \le speed \le 180 degrees per second. The speed is an integer.

Each wedge is given by a start angle and an extent (its angle size), both integers in degrees. Wedges in the test data are separated by at least one degree.

At time 0, the alignment marks of all wheels line up. Find the earliest integer time (in seconds) after the start at which a wedge on each wheel lines up with the wedges on the other wheels, so that a light beam can pass through the openings in all five wheels. The wedges can line up at any point in the rotation.

Input

Each of the five lines describes one wheel. The first integer on a line is the wheel's rotation speed. The next integer is the number of wedges WW, where 1≤W≤51 \le W \le 5. The next WW pairs of integers give each wedge's start angle and extent.

Output

Print a single integer: the first time the wedges line up so that a light beam can pass through all five wheels. Print 'none' (lowercase, without quotes) if the wedges never line up.

Examples1

  1. Example 1

    Input
    30 1 0 120
    50 1 150 90
    60 1 60 90
    70 1 180 180
    90 1 180 60
    
    Expected output
    9