#include <stdio.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)

int main(){
	ll a,b,c,r,n;
	while(scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&r),n){
		int cnt=0;
		while(n--){
			ll t;
			scanf("%ld",&t);
			while(cnt<=10010&&r!=t){
				r=(a*r+b)%c;
				cnt++;
			}
			r=(a*r+b)%c;
			cnt++;
		}
		printf("%d\n",cnt<=10001?cnt-1:-1);
	}
}
