fun main(args:Array<String>?): Unit {
    while(true){
        val (n, a, b, c, x) = readLine()!!.trim().split(' ').map(String::toInt)
        if (n == 0) return
        val lastCount = IntArray(c){-1}
        lastCount[x] = 0
        var current = x
        while(lastCount[(current * a + b) % c] == -1){
            lastCount[(current * a + b) % c] = lastCount[current] + 1
            current = (current * a + b) % c
        }
        val loopStart = lastCount[(current * a + b) % c]
        val loopLength = lastCount[current] + 1 - loopStart
        println(
                readLine()!!.trim().split(' ').map(String::toInt).fold(-1 as Int?){prev, target ->
                    prev?.let{frame ->
                        target.takeIf { target < c && lastCount[target] != -1}?.let{count ->
                            when {
                                lastCount[count] > frame -> lastCount[count]
                                lastCount[count] >= loopStart ->  (frame - lastCount[count] + loopLength) / loopLength * loopLength + lastCount[count]
                                else -> null
                            }
                        }
                    }
                }?.takeIf{it <= 10000} ?: -1
        )

    }
}
