Alien Sunset

Find the earliest time when every settlement is dark on its own periodic day, or report that none exists within the term.

Medium5MathNumber theoryBrute forceNo attempts yetTime limit2sMemory limit512 MB

Problem

Humans have settled planets, moons, asteroids, and other oddly shaped rocks all over the solar system. To mark the occasion the Galactic President declares a new holiday called Solar Night. At the moment of the celebration every settlement launches fireworks at once, so every settlement has to be in darkness at that same moment.

Night is awkward in space. Each settlement has its own day length and its own hours of sunrise and sunset. Every settlement did start its clock at the same moment. A settlement may have started in daylight or in darkness, so the first recorded sunrise can come before or after the first recorded sunset.

Time is counted in whole hours t=0,1,2,t = 0, 1, 2, \dots from the moment the clocks started. A settlement whose solar day is HH hours long is at local hour tmodHt \bmod H after tt hours.

The President's term lasts exactly 18251825 days as measured by the body with the longest rotation period, so the celebration must happen within the first 1825×Hmax1825 \times H_{\max} hours, where HmaxH_{\max} is the largest day length among the settlements.

Find the earliest hour at which every settlement is in darkness.

Input

The first line contains the integer NN (1N201 \le N \le 20), the number of settlements.

Each of the next NN lines contains three integers.

  • HH (2H1002 \le H \le 100), the number of hours in this settlement's solar day.
  • RR and TT (0R,TH10 \le R, T \le H - 1, RTR \ne T), the hour of sunrise and the hour of sunset.

A settlement is in darkness at local hour RR and at local hour TT. It is in daylight at the local hours strictly between sunrise and sunset, counting forward from RR until TT is reached. When T<RT < R the count wraps past the end of the day and continues at hour 00. Every other local hour is darkness.

Output

Print the smallest tt with 0t<1825×Hmax0 \le t < 1825 \times H_{\max} such that every settlement is in darkness tt hours after the clocks started. If no such tt exists, print impossible.