#include <stdio.h>

int main() {
  while (1) {
    int N, A, B, C, X;
    scanf("%d %d %d %d %d", &N, &A, &B, &C, &X);
    if (N == 0 && A == 0 && B == 0 && C == 0 && X == 0) break;
    int Y[100];
    for (int i = 0; i < N; i++) scanf("%d", &Y[i]);
    int stopped = 0, frames = -1;
    for (int i = 0; i <= 10000; i++) {
      if (X == Y[stopped]) {
        stopped++;
        if (stopped == N) frames = i;
      }
      X = (A * X + B) % C;
    }
    printf("%d\n", frames);
  }
}

