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 MBHumans 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,… from the moment the clocks started. A settlement whose solar day is H hours long is at local hour tmodH after t hours.
The President's term lasts exactly 1825 days as measured by the body with the longest rotation period, so the celebration must happen within the first 1825×Hmax hours, where Hmax is the largest day length among the settlements.
Find the earliest hour at which every settlement is in darkness.
The first line contains the integer N (1≤N≤20), the number of settlements.
Each of the next N lines contains three integers.
A settlement is in darkness at local hour R and at local hour T. It is in daylight at the local hours strictly between sunrise and sunset, counting forward from R until T is reached. When T<R the count wraps past the end of the day and continues at hour 0. Every other local hour is darkness.
Print the smallest t with 0≤t<1825×Hmax such that every settlement is in darkness t hours after the clocks started. If no such t exists, print impossible.