#include <stdio.h>
#include <string.h>
int p,e,i,d;
int main()
{
int a,b,c;
int time,sum;
int tot;
int j;
a=23*28;
b=23*33;
c=28*33;
sum=23*33*28;
time=0;
while(scanf("%d%d%d%d",&p,&e,&i,&d)!=-1)
{
if(p==-1&&e==-1&&i==-1&&d==-1)
break;
p%=23; //除23的余数
e%=28; //除28的余数
i%=33; //除33的余数
tot=0;
j=1;
while(a*j%33!=1)
j++;
tot+=a*j*i; //k1*m1
j=1;
while(b*j%28!=1)
j++;
tot+=b*j*e; //k2*m2
j=1;
while(c*j%23!=1)
j++;
tot+=c*j*p; //k3*m3
tot+=sum;
tot-=d;
if(tot%sum!=0)
tot%=sum;
else
tot=sum;
time++;
printf("Case %d: the next triple peak occurs in %d days.\n",time,tot);
}
return 0;
}
#include <string.h>
int p,e,i,d;
int main()
{
int a,b,c;
int time,sum;
int tot;
int j;
a=23*28;
b=23*33;
c=28*33;
sum=23*33*28;
time=0;
while(scanf("%d%d%d%d",&p,&e,&i,&d)!=-1)
{
if(p==-1&&e==-1&&i==-1&&d==-1)
break;
p%=23; //除23的余数
e%=28; //除28的余数
i%=33; //除33的余数
tot=0;
j=1;
while(a*j%33!=1)
j++;
tot+=a*j*i; //k1*m1
j=1;
while(b*j%28!=1)
j++;
tot+=b*j*e; //k2*m2
j=1;
while(c*j%23!=1)
j++;
tot+=c*j*p; //k3*m3
tot+=sum;
tot-=d;
if(tot%sum!=0)
tot%=sum;
else
tot=sum;
time++;
printf("Case %d: the next triple peak occurs in %d days.\n",time,tot);
}
return 0;
}