#include<stdio.h>
int main(){
  int n,a,b,c,r,s=0,d[1000],i;
  while(scanf("%d %d %d %d %d",&n,&a,&b,&c,&r),n){
    for(i=s=0;i<n;i++)scanf("%d",&d[i]);
    for(i=0;i<10001;i++){
      if(d[s]==r)s++;
      if(s==n)break;
      r=(a*r+b)%c;
    }
    printf("%d\n",i-10001?i:-1);
  }
  return 0;
}