Under construction

Still being built. Things may change

Biorhythms

Time limit1sMemory limit128 MB

Summary
Given one peak day for each of the 23, 28, and 33 day cycles and a date d, find the days from d to the next day when all three peak together.
Level

Easy3 of 10

Topics
Number theory, Math
Solved
No attempts yet

Problem

Some people believe that three cycles start on the day a person is born and then run for life. The three cycles are physical, emotional, and intellectual, with periods of 23, 28, and 33 days. Each period of a cycle contains exactly one peak. On the peak day a person performs at his or her best in the matching area. When the intellectual cycle peaks, for example, thinking is sharper and concentration comes more easily.

Because the three periods differ, the peaks usually land on different days. A day on which all three peaks land together is a triple peak, and we want to know when one happens. For each cycle you are given one day on which it peaks, counted in days from the beginning of the current year. That day is not necessarily the first peak. You are also given a date, counted the same way. Find how many days pass from the given date to the next triple peak. The given date itself is not counted. If the given date is 10 and the next triple peak falls on day 12, the answer is 2, not 3. If a triple peak falls on the given date, report the number of days to the following triple peak.

Input

The input holds several test cases. Each test case is one line with four integers p, e, i, and d separated by spaces. The values p, e, and i are the days on which the physical, emotional, and intellectual cycles peak, counted from the beginning of the current year. The value d is the given date and may be smaller than p, e, or i. All four values are between 0 and 365, and a triple peak is guaranteed to occur within 21252 days of the given date. A line whose four values are all -1 ends the input.

Output

For each test case, print the case number and the number of days to the next triple peak in this form.

Case x: the next triple peak occurs in y days.

Replace x with the case number, counting from 1, and y with the number of days.

Examples2

  1. Example 1

    Input
    0 0 0 0
    0 0 0 100
    5 20 34 325
    4 5 6 7
    283 102 23 320
    203 301 203 40
    -1 -1 -1 -1
    
    Expected output
    Case 1: the next triple peak occurs in 21252 days.
    Case 2: the next triple peak occurs in 21152 days.
    Case 3: the next triple peak occurs in 19575 days.
    Case 4: the next triple peak occurs in 16994 days.
    Case 5: the next triple peak occurs in 8910 days.
    Case 6: the next triple peak occurs in 10789 days.
    
  2. Example 2

    Input
    1 1 1 1
    -1 -1 -1 -1
    
    Expected output
    Case 1: the next triple peak occurs in 21252 days.