#include <stdio.h>

int next(int A, int B, int C, int X){
    return (A*X+B)%C;
}

int main(void){
    int N,A,B,C,X,i,j,k,Y,count;
    while(scanf("%d %d %d %d %d",&N,&A,&B,&C,&X),N||A||B||C||X){
        //printf("N:%d A:%d B:%d C:%d X:%d\n",N,A,B,C,X);
        count=0;
        for(i=0;i<N;i++){
            scanf("%d",&Y);
            //printf("Y%d:%d\n",i,Y);
            while(~count){
                count++;
                if(Y==X){
                    X=next(A,B,C,X);
                    break;
                }else{
                    X=next(A,B,C,X);
                }
                if(count>10000) count=-1;
            }
        }
        if(~count)count--;
        printf("%d\n",count);
    }
    
    return 0;
}