#include
<
iostream
>
using
namespace
std;
int
M[
3
]
=
{
23
,
28
,
33
}, A[
3
], NOW;
int
exGCD(
int
a,
int
b,
int
&
x,
int
&
y)
{
if
(b
==
0
)
{
x
=
1
;
y
=
0
;
return
a;
}
int
r
=
exGCD(b, a
%
b, x, y);
int
t
=
x;
x
=
y;
y
=
t
-
a
/
b
*
y;
return
r;
}
int
china(
int
n)
{
int
m
=
1
, ans
=
0
, i, mi, pi, qi;
for
(i
=
0
; i
<
n; i
++
)
m
*=
M[i];
for
(i
=
0
; i
<
n; i
++
)
{
mi
=
m
/
M[i];
exGCD(mi, M[i], pi, qi);
ans
=
(ans
+
mi
*
pi
*
A[i])
%
m;
}
ans
-=
NOW;
while
(ans
<=
0
)
ans
+=
m;
return
ans;
}
int
main()
{
int
cas
=
1
;
while
(scanf(
"
%d %d %d %d
"
,
&
A[
0
],
&
A[
1
],
&
A[
2
],
&
NOW)
!=
EOF)
{
if
(A[
0
]
==
-
1
&&
A[
1
]
==
-
1
&&
A[
2
]
==
-
1
&&
NOW
==
-
1
)
break
;
for
(
int
i
=
0
; i
<
3
; i
++
)
A[i]
%=
M[i];
printf(
"
Case %d: the next triple peak occurs in %d days.\n
"
, cas
++
, china(
3
));
}
return
0
;
}