Kitamasa is studying various transformations at university. Recently, the transformations that Kitamasa is interested in are as follows. N + 1 integers a0, ..., aN are fixed, and the integer z is used as an input. Also, let P be a prime number.

> t = (aNzN + aN-1zN-1 + ... + a2z2 + a1z + a0) mod P

Kitamasa noticed that there are a number of z that result in t = 0 due to this transformation. So, Kitamasa decided to ask you, a friend and a super programmer, to calculate how many z's from 0 to P-1 would be t = 0 after conversion.

Notes on Test Cases

Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format.

When both N and P are 0, the end of input is indicated.

<!-



Input

The input is given in the following format:


N P
a0 a1 ... aN

The first line of input is given an integer N and a prime number P separated by a space character. These are as given in the question sentence. Satisfy 0 ≤ N ≤ 100, 2 ≤ P ≤ 109.

The next line is given a0 to aN separated by a space character. These are integers that satisfy | ai | ≤ 109. Also, aN ≠ 0.

Output

Output the number of z (0 ≤ z <P) such that it becomes 0 after conversion.

Examples

Input

2 3
1 2 1
2 3
1 2 6
0 0


Output

1
1


Input

2 3
1 2 1


Output

1


Input

2 3
1 2 6


Output

1
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;


// Fp体上の多項式（MOD = P）
inline long long mod(long long a, long long m) {
	return (a % m + m) % m;
}

long long pow(long long a, long long n, long long m) {
	if (n == 0) return 1 % m;
	long long t = pow(a, n / 2, m);
	t = mod(t * t, m);
	if (n & 1) t = mod(t * a, m);
	return t;
}

long long inv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t*b; swap(a, b);
		u -= t*v; swap(u, v);
	}
	return mod(u, m);
}

struct ModPolynomial : vector<long long> {
	long long MOD = 1000000007;                                              // to be set
	ModPolynomial() : vector<long long>(1, 0) { }
	ModPolynomial(long long num) : vector<long long>(1, num) { }
	ModPolynomial(int size, long long num) : vector<long long>(size, num) { }
	ModPolynomial(vector<long long> vec) { (*this) = vec; }

	void init(long long num) { *this = vector<long long>(1, num); }
	void init(int size, long long num) { *this = vector<long long>(size, num); }
	void init(vector<long long> vec) { *this = vec; }
	ModPolynomial& normalize() {
		for (int i = 0; i < (*this).size(); ++i) (*this)[i] = mod((*this)[i], MOD);
		while ((*this).size() > 1 && (*this).back() == 0) (*this).pop_back();
		return (*this);
	}

	inline ModPolynomial operator - () {
		for (int i = 0; i < (*this).size(); ++i) (*this)[i] = (*this)[i] * (-1);
		return (*this).normalize();
	}
	inline const ModPolynomial& operator += (const ModPolynomial &x);
	inline const ModPolynomial& operator -= (const ModPolynomial &x);
	inline const ModPolynomial& operator *= (long long x);
	inline const ModPolynomial& operator *= (const ModPolynomial &x);
	inline const ModPolynomial& operator /= (long long x);
	inline const ModPolynomial& operator /= (const ModPolynomial &x);
	inline const ModPolynomial& operator %= (long long x);
	inline const ModPolynomial& operator %= (const ModPolynomial &x);
};

inline bool operator > (ModPolynomial x, ModPolynomial y) { return x.size() > y.size(); }
inline bool operator < (ModPolynomial x, ModPolynomial y) { return y > x; }
inline bool operator <= (ModPolynomial x, ModPolynomial y) { return !(y < x); }
inline bool operator >= (ModPolynomial x, ModPolynomial y) { return !(x < y); }
inline bool operator != (ModPolynomial x, ModPolynomial y) { return x < y || y < x; }
inline bool operator == (ModPolynomial x, ModPolynomial y) { return !(x < y) && !(y < x); }

ostream &operator << (ostream &os, ModPolynomial x) {
	for (int i = (int)x.size() - 1; i >= 0; --i) {
		if (i != (int)x.size() - 1) os << abs(x[i]); else os << x[i];
		if (i != 0) {
			os << "x"; if (i != 1) os << "^" << i; os << " ";
			if (x[i - 1] < 0) os << "- "; else os << "+ ";
		}
	}
	return os;
}

inline ModPolynomial operator + (ModPolynomial x, ModPolynomial y) {
	x.normalize(); y.normalize();
	while (x.size() > y.size()) y.push_back(0);
	while (y.size() > x.size()) x.push_back(0);
	ModPolynomial z((int)x.size(), 0);
	z.MOD = x.MOD;
	for (int i = 0; i < (int)x.size(); ++i) z[i] = x[i] + y[i];
	return z.normalize();
}
inline ModPolynomial operator - (ModPolynomial x, ModPolynomial y) {
	y = -y;
	return x + y;
}
inline ModPolynomial operator * (ModPolynomial x, long long a) {
	x.normalize();
	ModPolynomial z((int)x.size(), 0);
	z.MOD = x.MOD;
	for (int i = 0; i < (int)x.size(); ++i) z[i] = mod(x[i] * a, x.MOD);
	return z.normalize();
}
inline ModPolynomial operator * (ModPolynomial x, ModPolynomial y) {
	x.normalize(); y.normalize();
	ModPolynomial z((int)x.size() + (int)y.size() - 1, 0);
	z.MOD = x.MOD;
	for (int i = 0; i < (int)x.size(); ++i) for (int j = 0; j < (int)y.size(); ++j) z[i + j] = mod(z[i + j] + x[i] * y[j], z.MOD);
	return z.normalize();
}
pair<ModPolynomial, long long> divmod(ModPolynomial x, long long a) {
	x.normalize();
	for (int i = (int)x.size() - 1; i >= 0; --i) x[i] = mod(x[i] * inv(a, x.MOD), x.MOD);
	return pair<ModPolynomial, long long>(x, 0);
}
ModPolynomial operator / (ModPolynomial x, long long a) {
	x.normalize();
	return divmod(x, a).first;
}
long long operator % (ModPolynomial x, long long a) {
	x.normalize();
	return divmod(x, a).second;
}
pair<ModPolynomial, ModPolynomial> divmod(ModPolynomial x, ModPolynomial y) {
	x.normalize(); y.normalize();
	if (x.size() < y.size()) return make_pair(0, x);
	if (y.size() == 1) return divmod(x, y.back());
	ModPolynomial q((int)x.size() - (int)y.size() + 1, 0);
	for (int i = (int)x.size() - 1; i >= (int)y.size() - 1; --i) {
		long long div = mod(x[i] * inv(y.back(), x.MOD), x.MOD);
		q[i - (int)y.size() + 1] = div;
		for (int j = 0; j < (int)y.size(); ++j) x[i + j - (int)y.size() + 1] = mod(x[i + j - (int)y.size() + 1] - y[j] * div, x.MOD);
	}
	return make_pair(q, x.normalize());
}
ModPolynomial operator / (ModPolynomial x, ModPolynomial y) {
	x.normalize(); y.normalize();
	return divmod(x, y).first;
}
ModPolynomial operator % (ModPolynomial x, ModPolynomial y) {
	x.normalize(); y.normalize();
	return divmod(x, y).second;
}
ModPolynomial pow(ModPolynomial a, long long n) {
	a.normalize();
	ModPolynomial res(1, 1);
	res.MOD = a.MOD;
	while (n > 0) { if (n & 1) { res = res * a; } a = a * a; n >>= 1; }
	return res;
}
ModPolynomial pow(ModPolynomial a, long long n, ModPolynomial PMOD) {
	a.normalize(); PMOD.normalize();
	ModPolynomial res(1, 1);
	res.MOD = a.MOD;
	while (n > 0) { if (n & 1) { res = (res * a) % PMOD; } a = (a * a) % PMOD; n >>= 1; }
	return res;
}
ModPolynomial gcd(ModPolynomial x, ModPolynomial y) {
	x.normalize(); y.normalize();
	if (y.size() == 1 && y.back() == 0) return x;
	else return gcd(y, x%y);
}
inline const ModPolynomial& ModPolynomial::operator += (const ModPolynomial &x) { *this = *this + x; return *this; }
inline const ModPolynomial& ModPolynomial::operator -= (const ModPolynomial &x) { *this = *this - x; return *this; }
inline const ModPolynomial& ModPolynomial::operator *= (long long x) { *this = *this * x; return *this; }
inline const ModPolynomial& ModPolynomial::operator *= (const ModPolynomial &x) { *this = *this * x; return *this; }
inline const ModPolynomial& ModPolynomial::operator /= (long long x) { *this = *this / x; return *this; }
inline const ModPolynomial& ModPolynomial::operator /= (const ModPolynomial &x) { *this = *this / x; return *this; }
inline const ModPolynomial& ModPolynomial::operator %= (long long x) { *this = *this % x; return *this; }
inline const ModPolynomial& ModPolynomial::operator %= (const ModPolynomial &x) { *this = *this % x; return *this; }




int main() {
	long long N, P;
	while (cin >> N >> P) {
		if (N == 0) break;

		ModPolynomial f(N + 1, 0);
		f.MOD = P;
		for (int i = 0; i < N + 1; ++i) cin >> f[i];
		f.normalize();

		if (f.size() == 1 && f.back() == 0) { 
			cout << P << endl;
			continue; 
		}

		ModPolynomial x(2, 0); // 多項式 x
		x.MOD = P;
		x[1] = 1;
		ModPolynomial xp_minus_x = pow(x, P, f) - x;
		ModPolynomial g = gcd(f, xp_minus_x);

		cout << (int)g.size() - 1 << endl;
	}
}
