function Main(input){
    input = input.split("\n");
    var k = 0;
    while(1){
        input[k] = input[k].split(" ");
        var n = parseInt(input[k][0],10);
        var a = parseInt(input[k][1],10);
        var b = parseInt(input[k][2],10);
        var c = parseInt(input[k][3],10);
        var x = parseInt(input[k++][4],10);

        if(n == 0 && a == 0 && b == 0 && c == 0 && x == 0)
            break;
        input[k] = input[k].split(" ");
        var y = [];
        for(var i = 0;i < n;i++)
            y[i] = parseInt(input[k][i],10);
        k++;

        var frame = 0,flag = true,now = 0;

        while(frame <= 10000){
            if(x === y[now]){
                now++;
                if(now == n){
                    console.log(frame);
                    flag = false;
                    break;
                }
            }
            x = (a * x + b) % c;
            frame++;
        }
        if(flag)
            console.log("-1");
    }
}
Main(require("fs").readFileSync("/dev/stdin","utf8"));