loop do
    n, a, b, c, x = gets.split.map(&:to_i)
    break if n == 0 && a == 0 && b == 0 && c == 0 && x == 0
    y = gets.split.map(&:to_i)
    i = 0
    bl = false
    10001.times do |count|
        if y[i] == x
            i += 1
        end
        if i == n
            puts count
            bl = true
            break
        end
        x = (a * x + b) % c
    end
    next if bl
    puts "-1"
end