main()
{
  int n,a,b,c,x,y,f,t;
  while(1){
    scanf("%d%d%d%d%d",&n,&a,&b,&c,&x);
    if(!n)break;
    for(f=0,t=1;n-->0;){
    scanf("%d",&y);
    while(t==0||x!=y&&f<10001){  
      t=1;
      //	int tmp;
      f++;
      //	tmp= x;
       x=(a*x+b)%c;
       //	printf("x'(%d) = (%d* %d +%d)mod %d\t%dt[Ú\n",x,a,tmp,b,c,f);
     }
    t=0;
    }
    if(f>10000)
      f=-1;
    printf("%d\n",f);
  }
}