Clock
Residents of the planet Olympia enjoy visiting other planets. Scientists there have developed a clock that can be adjusted to track time on any planet. This clock uses balls, a tray (queue), and three cups: one for seconds, one for minutes, and one for hours. At any moment, the number of balls in these cups indicates the current time in seconds, minutes, and hours, respectively.
Every second, the first ball from the queue is placed into the second cup. If the second cup becomes full (meaning it contains as many balls as there are seconds in a minute on this planet), one ball moves to the minute cup, and the remaining balls from the second cup are returned to the end of the queue in reverse order of their arrival. Similarly, when the minute cup fills up, the last ball moves to the hour cup, and the remaining balls from the minute cup are returned to the end of the queue in reverse order. If the hour cup fills up, all balls from it are returned to the end of the queue in reverse order of their arrival. All balls are numbered and initially start in the queue.
Your task is to write a program that calculates the minimum number of days required for the initial arrangement of the balls in the queue to repeat.
Input
The input file contains a single line with four natural numbers: S, M, H, and K. These represent the number of seconds in a minute, minutes in an hour, hours in a day, and the total number of balls, respectively. The constraints are as follows:
S, M, H ≤ 60;
S+M+H-2 ≤ K ≤ 1000
Output
The output file should contain a single line with the number of days calculated by your program.