Alien Sunset
Time limit2sMemory limit512 MB
Find the earliest time when every settlement is dark on its own periodic day, or report that none exists within the term.
- Level
Medium5 of 10
- Topics
- Math, Number theory, Brute force
- Solved
- No attempts yet
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 from the moment the clocks started. A settlement whose solar day is hours long is at local hour after hours.
The President's term lasts exactly days as measured by the body with the longest rotation period, so the celebration must happen within the first hours, where 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 (), the number of settlements.
Each of the next lines contains three integers.
- (), the number of hours in this settlement's solar day.
- and (, ), the hour of sunrise and the hour of sunset.
A settlement is in darkness at local hour and at local hour . It is in daylight at the local hours strictly between sunrise and sunset, counting forward from until is reached. When the count wraps past the end of the day and continues at hour . Every other local hour is darkness.
Output
Print the smallest with such that every settlement is in darkness hours after the clocks started. If no such exists, print impossible.