#include <stdio.h>
int main(){
  int n,a,b,c,f,i,x,y,z[101];
  while(1){
    i=1;
    f=0;
    scanf("%d %d %d %d %d",&n,&a,&b,&c,&x);
    if(n==0 && a==0 && b==0 && c==0 && x==0)break;
    for(n;n>0;n--){
      scanf("%d",&z[i]);
      while(1){
	y=(a*x+b)%c;
	x=y;
	f++;
	if(y==z[i]){
	  i++;
	  break;
	}
      }
    }
    if(f<=10000){
      printf("%d\n",f);
    }
    if(f>10000){
      printf("-1\n");
    }
  }
  return 0;
}