/*
x-a = 23*a';
x-b = 28*b';
x-c = 33*c';
x === a mod 23
x === b mod 28
x === c mod 33
求x,这是中国剩余定理,
这里应用要求m1,m2,m3两两互质
x mod 23 = a mod 23
x mod 28 = b mod 28
x mod 33 = c mod 33
需要用欧拉函数了phi(n)
m = 23*28*33
M1 = m/m1 = 28*33
M2 = m/m2 = 23*33
M3 = m/m3 = 23*28
M1' = M1^(phi(m1)-1) mod m1
M2' = M2^(phi(m2)-1) mod m2
M3' = M3^(phi(m3)-1) mod m3
x = (M1*M1'*a+M2*M2'*b+M3*M3'*c) mod m
*/
// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>
using namespace std;
// typedef
typedef __int64 LL;
//
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define Z(a,b) ((a)<<(b))
#define Y(a,b) ((a)>>(b))
const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 1000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
if(x>y) return x;
return y;
}
template<class T> inline T TMIN(T x,T y)
{
if(x<y) return x;
return y;
}
template<class T> inline T MMAX(T x,T y,T z)
{
return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
return TMIN(TMIN(x,y),z);
}
template<class T> inline void SWAP(T &x,T &y)
{
T t = x;
x = y;
y = t;
}
// code begin
int phi[50];
int a,b,c,d,M1,M2,M3,M11,M22,M33,m;
int f(int M_,int M_n,int M_mod)
{
int ans = 1;
for(int i=0;i<M_n;i++)
{
ans *= M_;
ans %= M_mod;
}
return ans;
}
int main()
{
read;
write;
phi[23] = 22; //
phi[28] = 12; // 28*(1-1/2)*(1-1/7) =
phi[33] = 20; // 33*(1-1/3)*(1-1/11) = 20
int ans,cas=1;
while(scanf("%d %d %d %d",&a,&b,&c,&d)==4)
{
if(a==-1 && b==-1 && c==-1 && d==-1)
break;
a%=23;
b%=28;
c%=33;
m = 23*28*33;
M1 = m/23;
M2 = m/28;
M3 = m/33;
M11 = f(M1,phi[23]-1,23);
M22 = f(M2,phi[28]-1,28);
M33 = f(M3,phi[33]-1,33);
ans = (M1*M11*a+M2*M22*b+M3*M33*c)%m;
if(ans-d<=0) ans+=m;
printf("Case %d: the next triple peak occurs in %d days.\n",cas++,ans-d);
}
return 0;
}