import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;

void main()
{
	foreach (line; stdin.byLine) {
		auto inp = line.chomp.split.map!(to!int);
		if (inp[0] == 0) break;
		immutable a = inp[1],
		b = inp[2],
		c = inp[3];
		auto x = inp[4];
		auto tar = readln.chomp.split.map!(to!int);
		int cnt, fi;
		while (cnt <= 10000) {
			if (tar[fi] == x) fi++;
			if (fi == inp[0]) break;
			cnt++;
			x = (a*x+b) % c;
		}
		if (cnt == 10001) writeln(-1);
		else writeln(cnt);
	}
}