#include<stdio.h> #include<string.h> int vis[10005]; int main(){ int Z, I, M, L, t = 0; while(scanf("%d%d%d%d", &Z, &I, &M, &L) && Z && I && M && L){ memset(vis, 0, sizeof(vis)); int cnt = 0, k; while (1){ L = (Z * L + I) % M; if (vis[L]) break; cnt++; vis[L] = 1; } printf("Case %d: %d\n", ++t, cnt); } return 0; }