#P2728. Aligned Notches

    ID: 15991 Type: Default 1000ms 256MiB

Aligned Notches

Aligned Notches

This problem involves a set of 5 rotating wheels. Each wheel rotates by a fixed integer speed (in degrees per second) and has a number of notches (gaps) that are defined by their starting angle and width (in degrees). The angles on the wheels wrap around modulo 360; that is, after 359 comes 0.

Each notch on a wheel is defined by two integers: its starting angle and its width. Note that the notch covers width consecutive angles beginning with the starting angle, and the starting angle is included. For example, a notch defined as "0 179" covers 180 angles: 0 through 179. Also, no two notches on the same wheel are adjacent; there is at least 1 degree separating any two notches.

At time 0 all wheels are aligned so that their starting marks lie along a straight line. As time progresses the wheels rotate. At time t (in seconds), for each notch on a wheel with initial starting position S and speed V, the effective starting angle becomes \( (S+V \cdot t) \mod 360\). If the notch (of width W) goes past 359 it wraps around (i.e. it covers two intervals).

Your task is to determine the earliest time (in seconds) when there exists an angle (0° to 359°) that is simultaneously open (i.e. falls into at least one notch) on all 5 wheels. If there is no such time within 0 to 359 seconds, output none.

Note: All input values and computations are in integers; wheels do not rotate by fractional amounts.

inputFormat

The input consists of 5 lines, one for each wheel. Each line begins with an integer indicating the wheel's rotation speed (in degrees per second, 1 ≤ speed ≤ 180), followed by an integer N indicating the number of notches. Then follow N pairs of integers. Each pair represents a notch as "start width" (in degrees). Both start and width are integers (0 ≤ start ≤ 359). The notch covers the angles from start to start+width-1 (modulo 360). There is at least 1 degree of separation between notches on the same wheel.

outputFormat

Output the earliest time (in seconds) when a single angle exists that is open on all 5 wheels. If no such time exists within 0 to 359 seconds, output none.

sample

1 1 0 180
2 1 50 100
3 1 100 100
4 1 120 30
5 1 130 20
0