#include<stdio.h>
#include<string.h>
#include<stdlib.h>


int main() {

	int N,A,B,C,X;
	int *Y;
	int i,count,pos;
	int Answer[1000],a=0;

	for(;;) {
		scanf("%d %d %d %d %d",&N,&A,&B,&C,&X);
		if(N==0&&A==0&&B==0&&C==0&&X==0) break;
		Y=(int *)malloc(N*sizeof(int));

		for(i=0;i<N;i++)
			scanf("%d",&Y[i]);

		count=-1,pos=0;
		while(pos<N) {
			//printf("%d %d %d\n",pos,X,Y[pos]);
			if(X==Y[pos])
				pos++;
			X=(A*X+B)%C;
			count++;
			if(count>10000) {
				count=-1;
				break;
			}
		}
		Answer[a]=count;
		a++;
	}
	for(i=0;i<a;i++)
		printf("%d\n",Answer[i]);
	return 0;
}