#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;

class RightTriangle { public:
	long long triangleCount(int places, int points, int a, int b, int c) 
	{
		vector<bool> red(places);
		set<int> vacant;
		for(int i=0; i<places; ++i)
			vacant.insert(i);

		for(LL n=0; n<points; ++n) {
			LL p = (a*n*n + b*n + c) % places;
			if( red[p] ) {
				set<int>::iterator it = vacant.lower_bound(p);
				if( it != vacant.end() )
					p = *it;
				else {
					it = vacant.lower_bound(0);
					p = *it;
				}
			}
			red[p] = true;
			vacant.erase(p);
		}

		if( places%2 == 1 )
			return 0;

		LL result = 0;
		for(int a=0,b=a+places/2; b<places; ++a, ++b)
			if( red[a] && red[b] )
				result += points-2;
		return result;
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, RightTriangle().triangleCount(places, points, a, b, c));}
int main(){

CASE(0)
	int places = 9; 
	int points = 3; 
	int a = 0; 
	int b = 3; 
	int c = 0; 
	long long _ = 0LL; 
END
CASE(1)
	int places = 40; 
	int points = 3; 
	int a = 5; 
	int b = 0; 
	int c = 0; 
	long long _ = 1LL; 
END
CASE(2)
	int places = 4; 
	int points = 4; 
	int a = 16; 
	int b = 24; 
	int c = 17; 
	long long _ = 4LL; 
END
CASE(3)
	int places = 1000000; 
	int points = 47000; 
	int a = 0; 
	int b = 2; 
	int c = 5; 
	long long _ = 0LL; 
END
CASE(4)
	int places = 200000; 
	int points = 700; 
	int a = 123456; 
	int b = 789012; 
	int c = 345678; 
	long long _ = 6980LL; 
END
CASE(5)
	int places = 1000000; 
	int points = 0; 
	int a = 0; 
	int b = 0; 
	int c = 0; 
	long long _ = 0LL; 
END
CASE(6)
	int places = 1000000; 
	int points = 100000; 
	int a = 0; 
	int b = 0; 
	int c = 0; 
	long long _ = 0LL; 
END
CASE(7)
	int places = 1; 
	int points = 0; 
	int a = 1; 
	int b = 2; 
	int c = 3; 
	long long _ = 0LL; 
END
CASE(8)
	int places = 1; 
	int points = 1; 
	int a = 3; 
	int b = 2; 
	int c = 1; 
	long long _ = 0LL; 
END
CASE(9)
	int places = 3; 
	int points = 3; 
	int a = 1000000; 
	int b = 1000000; 
	int c = 1000000; 
	long long _ = 0LL; 
END
CASE(9)
	int places = 4; 
	int points = 4; 
	int a = 1000000; 
	int b = 1000000; 
	int c = 1000000; 
	long long _ = 4LL; 
END

}
// END CUT HERE
