loop {
    n, a, b, c, x = gets.split.map &:to_i
    break if n == 0
    target = gets.split.map &:to_i

    i = 0   
    10001.times {
        target.shift if target[0] == x
        break if target.empty?
        x = (a * x + b) % c 
        i += 1
    }   
    puts i > 10000 ? -1 : i 
}