/*
  AOJ 2213
  Title:The Number of Solutions for a Polynomial
  @kankichi573
  2014/3/11 TLE
*/
#include <stdio.h>
int N,P,a[101],beki_mod[101];
int calc_poly_mod(int a[],int beki_mod[],int n,int p,int beki_cycle)
{
  int i,sum;
  unsigned long long w;
  for(i=0,sum=0;i<=n;i++)
    {
      sum += (beki_mod[i % beki_cycle]*a[i])%p;
      //printf("sum=%d,(%d)",sum,beki_mod[i % beki_cycle]);
      sum %= p;
    }
  return(sum);
}
int prepare(int z,int P,int beki_mod[])
{
  int product,i;

  beki_mod[0]=1;
  if(z==1)
    return(1);
  product=1;i=0;
  do
    {
      product *= z;

      product %= P;
      //printf("%d:",product);
      beki_mod[++i]=product;
    }
  while(product!=1);
  return(i);
}
main()
{
  int i,z,cnt,cycle,res;

  while(scanf("%d %d",&N,&P) && (N||P))
    {
      for(i=0;i<=N;i++)
	scanf("%d",&a[i]);
      for(z=1,cnt=0;z<P;z++)
	{
	  cycle=prepare(z,P,beki_mod);
	  res=calc_poly_mod(a,beki_mod,N,P,cycle);
	  //printf("z=%d P=%d cy=%d res=%d\n",z,P,cycle,res);
	  if(0==res)
	    cnt++;
	}
      if(a[0]==0)
	cnt++;
      printf("%d\n",cnt);
    }
  return(0);
}