package middle;
import java.util.Scanner;
/**
*poj1006
* ac
* , , 。
* ,
* : , , , ; , ; , 。 ?
* :n%3=2;n%5=3;n%7=2; n
* : , , ,
* :(2*70+3*21+2*15)%105=23
* 70:a=5*7=35,70,105... a a%3=1, a=70
* 21:b=3*7=21,42,63,84,105... b b%5=1, b=21
* 15:c=3*5=15,30,45,60,75,90,105... c c%7=1, c=15
* 105:3*5*7=105
* @author NC
*/
public class Poj1006 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int k = 0;
while (scan.hasNext()) {
int p = scan.nextInt();
int e = scan.nextInt();
int i = scan.nextInt();
int d = scan.nextInt();
if (p == -1 && e == -1 && i == -1 && d == -1) {
break;
}
k++;
// int days = d + 1;
// while (true) {
// if ((days - p) % 23 == 0 && (days - e) % 28 == 0
// && (days - i) % 33 == 0) {
// break;
// }
// days++;
// }
/**
*5544:28*33=924,1848,2772,3696,4620,5544...5544%23=1;5544 23
*14421:23*33=759,1518,2277,...,14421...14421%28=1;14421 28
*1288: 23*28=644,1288...1288%33=1;1288 33
*/
int days = (5544 * p + 14421 * e + 1288 * i - d) % (21252);
if (days <= 0) {
days = 23 * 28 * 33 + days;
}
System.out.println("Case " + k
+ ": the next triple peak occurs in " + days
+ " days.");
}
}
}