Clock Hands
Time limit5sMemory limit128 MB
The program finds the earliest time at or after the given time when the second hand bisects the angle between the other two hands with no overlap.
Problem
An analog clock has three hands (the second hand, the minute hand and the hour hand) that all rotate smoothly. You can measure the two angles between the second hand and the two other hands.
Write a program that finds the earliest time, at or after a given time, where "no two hands overlap each other" and "the two angles between the second hand and the two other hands are equal" both hold.

Figure D.1. Angles between the second hand and the two other hands
Clocks are not limited to 12-hour clocks. The hour hand of an -hour clock goes around once in hours. The minute hand still goes around once every hour, and the second hand goes around once every minute. At 0:0:0 (midnight) all three hands point straight up.
Input
The input consists of multiple datasets. Each dataset is one line with four integers , , and , separated by a space. means the clock is an -hour clock, and , , are the hour, minute and second of the specified time.
You may assume , , and .
The end of the input is a line containing four zeros.

Figure D.2. Examples of an -hour clock (a 6-hour clock and a 15-hour clock)
Output
For each dataset, output on one line the time at which "no two hands overlap each other" and "the two angles between the second hand and the two other hands are equal" hold for the first time at or after the specified time.
For being :: ( seconds past minutes past o'clock), output four non-negative integers , , and , separated by a space, where is the irreducible fraction representing . When is an integer, including 0, let be 1.
The time is expressed as the remainder modulo hours. In other words, one second after :59:59 is 0:0:0, not :0:0.