#include <iostream>
using namespace std;
int main(){
int Z, I, M, L ;
int temp, count ,next;
int index = 1 ;
while (1) {
scanf( "%d %d %d %d" , &Z, &I, &M, &L) ;
if (Z==0 && I==0 && M==0 && L==0) break;
count = 1 ;
next = temp = (Z*L+I)%M ;
while (temp != L) {
temp = (Z*temp+I)%M ;
if (temp == next) break;
count++;
}
printf( "Case %d: %d/n" , index++, count) ;
}
return 0;
}
using namespace std;
int main(){
int Z, I, M, L ;
int temp, count ,next;
int index = 1 ;
while (1) {
scanf( "%d %d %d %d" , &Z, &I, &M, &L) ;
if (Z==0 && I==0 && M==0 && L==0) break;
count = 1 ;
next = temp = (Z*L+I)%M ;
while (temp != L) {
temp = (Z*temp+I)%M ;
if (temp == next) break;
count++;
}
printf( "Case %d: %d/n" , index++, count) ;
}
return 0;
}