#include <stdio.h>
#define LIMIT 10000

/* ---------------------------------------------------------------------------- */

int A, B, C, X;

void my_srand(int aA, int aB, int aC, int aX)
{
    A = aA, B=aB, C=aC, X=aX;
}

int my_rand()
{
    int t = X;

    X = (A * X + B) % C;

    return t;
}

/* ---------------------------------------------------------------------------- */

int main(void)
{
    int n, a, b, c, x, t, count;

    while(scanf("%d %d %d %d %d", &n, &a, &b, &c, &x),
          n + a + b + c + x)
    {
        /* init */
        count = 1;
        my_srand(a, b, c, x);

        /* search */
        while(n--)
        {
            scanf("%d", &t);
            while(count <= LIMIT && t != my_rand()) count++;
        }

        /* result */
        if(count > LIMIT) puts("-1");
        else printf("%d\n", count);
    }

    return 0;
}