#include<iostream>
#include<string>
#include<cstdio>
#include<math.h>
#include<fstream>
using namespace std;
int answer[10000];
int main(){
int p=0,e=0,i=0,d=0;
int count=0;
while(1)
{
count++;//记录第几个数据
cin>>p>>e>>i>>d;
if(p==-1&&e==-1&&d==-1&&i==-1) break;
int x=d+1;
//if(x==p&&x==e&&x==i) {x+=1;}//eg:0 0 0 0
for(;;x++)
{
//if(x==p&&x==e&&x==i) {cout<<"Case "<<count<<": the next triple peak occurs in "<<x-d<<" days."<<endl;break;}
if(x==p&&x==e&&x==i) {answer[count]=x-d;break;}
while(1){if(x>=p+23) p=p+23;else break;}
while(1){if(x>=e+28) e=e+28;else break;}
while(1){if(x>=i+33) i=i+33;else break;}
if(x==p&&x==e&&x==i) {answer[count]=x-d;break;}
}
}
for(int y=1;y<count;y++)cout<<"Case "<<y<<": the next triple peak occurs in "<<answer[y]<<" days."<<endl;
system("pause");
return 0;
}