// AOJ 2149: Luck Manipulator
// 2017.11.9 bal4u@uu

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

int y[102];

int main()
{
	int n, a, b, c, x, i, j, ans;
	
	while (scanf("%d%d%d%d%d", &n, &a, &b, &c, &x) && n > 0) {
		for (i = 0; i < n; i++) scanf("%d", y+i);
		ans = -1, j = 0;
		i = 10001; while (i--) {
			if (y[j] == x) if (++j == n) { ans = 10000-i; break; }
			x = (a*x+b) % c;
		}
		printf("%d\n", ans);
	}
	return 0;
}