int extend_gcd(int a , int b , int &x , int &y){
if(b == 0){
x = 1 ;
y = 0 ;
return a ;
}
else{
int r = extend_gcd(b , a%b , y , x) ;
y -= x*(a/b) ;
return r ;
}
}
int CRT(int a[] , int m[] , int n){
int M = 1 ;
for(int i = 0 ; i < n ; i++) M *= m[i] ;
int ret = 0 ;
for(int i = 0 ; i < n ; i++){
int x , y ;
int tm = M/m[i] ;
extend_gcd(tm , m[i] , x , y) ;
ret = (ret + tm*x*a[i]) % M ;
}
return ret ;
}
int a[3] , m[3] = {23 , 28 , 33};
int main(){
int d , n = 23*28*33 , T = 1;
while(cin>>a[0]>>a[1]>>a[2]>>d){
if(a[0] == -1 && a[1] == -1 && a[2] == -1 && d == -1) break ;
int ans = CRT(a , m , 3) ;
ans = (ans - d) % n ;
if(ans <= 0) ans += n ;
printf("Case %d: the next triple peak occurs in %d days.\n" ,T++ , ans) ;
}
return 0 ;
}