import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

const real eps = 1e-10;

void main(){
	while(true) {
		auto l = readInts();
		auto N = l[0];
		auto A = l[1];
		auto B = l[2];
		auto C = l[3];
		auto X = l[4];
		if(N == 0) {
			return;
		}
		auto Y = readInts();
		int c = 0;
		auto n = 0;
		while(c <= 10000 && n < N) {
			if(X == Y[n]) {
				++n;
				if(n == N){
					break;
				}
			}
			X = (A * X + B) % C;
			++c;
		}
		if(n == N) {
			writeln(c);
		} else {
			writeln(-1);
		}
	}
}