Under construction

Still being built. Things may change

Traffic Lights

Interview

Time limit1sMemory limit2048 MB

Summary
Given n traffic lights with red and green durations that all turn red together, find the earliest time when every light is green again, or -1 if that never happens.
Level

Medium4 of 10

Topics
Simulation, Number theory
Solved
No attempts yet

Problem

A street has nn traffic lights. Each light alternates between rr seconds of red and gg seconds of green. The values of rr and gg can differ from light to light.

All the lights were green. Then all of them turned red at the same moment. Find how many seconds it takes until every light is green again.

Input

The first line contains a single integer nn (1≤n≤101 \le n \le 10), the number of traffic lights. Each of the next nn lines contains two integers rr and gg (1≤r,g≤101 \le r, g \le 10), the number of seconds that light stays red and green.

Output

Output a single integer, the earliest time at which all the lights are green again. If this never happens, output −1-1.

Examples1

  1. Example 1

    Input
    3
    2 4
    3 1
    6 1
    
    Expected output
    27