#include <stdio.h>

int main(void){
  int i,j;
  int n,a,b,c,x;
  int res;
  int y[102];

  while(scanf("%d%d%d%d%d",&n,&a,&b,&c,&x),n){
    for(i=0;i<n;i++){
      scanf("%d",&y[i]);
    }

    res = 0;
    for(i=0;i<n;i++,x=(a * x + b)%c,res++){
      while(y[i] != x){
        x = (a * x + b) % c;
        res++;
        if(res > 10000){
          res = -1;
          break;
        }
      }
      if(res == -1) break;
    }
    if(res != -1) res--;
    printf("%d\n",res);
  }

  return 0;
}