import std.stdio;
import std.string;
import std.conv;
import std.algorithm;

void main(){
  int n,a,b,c,X;
  while(readf("%d %d %d %d %d\n",&n,&a,&b,&c,&X),n||a||b||c||X){
    auto ran = map!("to!int(a)")(split(chomp(readln())));
    int x=X,j=0,i;
    for(i=0;i<=10000;++i){
      if(ran[j]==x) ++j;
      x=(a*x+b)%c;
      if(j==ran.length) break;
    }
    writeln((i==10001)?-1:i);
  }
}