#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

CMP tmp[65536*4];

void fft_impl( CMP a[], int stride, int n, double Wt )
{
	if( n > 1 )
	{
		CMP *ev=a, *od=a+stride;
		int s2=stride*2, n2=n/2;

		fft_impl( ev, s2, n2, Wt*2 );
		fft_impl( od, s2, n2, Wt*2 );

		for(int i=0; i<n; ++i) tmp[i] = ev[s2*(i%n2)] + od[s2*(i%n2)]*polar(1.0,i*Wt);
		for(int i=0; i<n; ++i) a[stride*i] = tmp[i];
	}
}

void fft( vector<CMP>& a )
{
	fft_impl(&a[0], 1, a.size(), +2*M_PI/a.size());
}

void ifft( vector<CMP>& a )
{
	fft_impl(&a[0], 1, a.size(), -2*M_PI/a.size());
	for(int i=0; i<a.size(); ++i)
		a[i] /= a.size();
}

class CircularShifts {
public:
	int maxScore(int N, int Z0, int A, int B, int M) 
	{
		// input
		LL X[60000], Y[60000], Z=Z0%M;
		for(int i=0; i<N+N; ++i)
		{
			(i<N ? X[i] : Y[i-N]) = Z % 100;
			Z = (Z*A+B) % M;
		}

		// solve
		int NN = 1;
		while( NN < N*2 ) NN*=2;

		vector<CMP> CX(NN), CY(NN), CZ(NN);
		for(int i=0; i<N; ++i)
			CX[i]=CX[i+N]=X[N-i-1], CY[i]=Y[i];

		fft(CX);
		fft(CY);
		transform( CX.begin(), CX.end(), CY.begin(), CZ.begin(), multiplies<CMP>() );
		ifft(CZ);

		double m = 0;
		for(int i=0; i<NN; ++i)
			m = max(m, CZ[i].real());
		return int(m+0.5);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int N = 5; 
	int Z0 = 1; 
	int A = 1; 
	int B = 0; 
	int M = 13; 
	int RetVal = 5; 
	return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); }
int Test_(Case_<1>) {
	int N = 4; 
	int Z0 = 1; 
	int A = 1; 
	int B = 1; 
	int M = 20; 
	int RetVal = 70; 
	return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); }
int Test_(Case_<2>) {
	int N = 10; 
	int Z0 = 23; 
	int A = 11; 
	int B = 51; 
	int M = 4322; 
	int RetVal = 28886; 
	return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); }
int Test_(Case_<3>) {
	int N = 1000; 
	int Z0 = 3252; 
	int A = 3458736; 
	int B = 233421; 
	int M = 111111111; 
	int RetVal = 2585408; 
	return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); }
int Test_(Case_<4>) {
	int N = 141; 
	int Z0 = 96478; 
	int A = 24834; 
	int B = 74860; 
	int M = 92112; 
	int RetVal = 419992; 
	return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE

