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 x != i && cnt != -1
      until x == i
        x = (a * x + b) % c
        cnt += 1
        break if cnt > 10000
      end
    end
    x = (a * x + b) % c
    if cnt == -1 then cnt
    elsif cnt <= 10000 then cnt + 1
    else -1
    end
  }
  p res >= 0 ? res - 1 : res
end