350

#include <iostream>
#include <map>


using namespace std;

map<unsigned long long,int> mp;

int main()
{
	unsigned long long z,I,M,L;
	int c=0;
	while(cin>>z)
	{
		cin>>I>>M>>L;

		if(z==0 && I==0 && M==0 && L==0)
		break;
		c++;
		mp.clear();

		L=((z)*(L)+I)%M;
		//mp[L]=1;
		while(mp[L]==0)
		{
			mp[L]=1;
			L=(z*L+I)%M;
		}
		cout<<"Case "<<c<<": ";
		cout<<mp.size()<<"\n";


	}

	return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *