#include <stdio.h>
#include <stdlib.h>
#define LIMIT 10000

int main(void){
    int n, a, b, c, x, next, cnt;
    int i, j;
    int result[LIMIT];
    j = 0;
    while(1){
        scanf("%d %d %d %d %d", &n, &a, &b, &c, &x);
        if(n == 0){
            break;
        }
        int *y = (int *)malloc(n * sizeof(int));
        for(i = 0; i < n; i++){
            scanf("%d", &y[i]);
        }

        i = 0;
        cnt = 0;
        while(1){
            if(y[i] == x){
                i++;
            }
            if(i == n){
                break;
            }
            else if(cnt == LIMIT){
                cnt = -1;
                break;
            }
            x = (a * x + b) % c;
            cnt++;
        }

        result[j] = cnt;
        j++;
    }

    for(i = 0; i < j; i++){
        printf("%d\n", result[i]);
    }


    return 0;
}