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


Measured memory_bytes: 3296