while gets
  n, a, b, c, x = $_.split.map(&:to_i)
  break if n == 0
  res = gets.split.map(&:to_i).reduce(0){ |cnt, i|
    if cnt == -1 || x == i then cnt
    else
      until x == i
        x = (a * x + b) % c
        cnt += 1
        break if cnt > 10000
      end
      x = (a * x + b) % c
      cnt > 10000 ? -1 : cnt + 1
    end
  }
  p res > 0 ? res - 1 : res
end