import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

void main() {
    while (true) {
        int n, a, b, c, x;
        scan(n, a, b, c, x);

        if (!n) return;

        auto y = readln.split.to!(int[]);
        solve(n, a, b, c, x, y);
    }
}

void solve(int n, int a, int b, int c, int x, int[] y) {
    int pos;

    foreach (i ; 0 .. 10^^4 + 1) {
        if (i > 0) x = nextrand(a, b, c, x);
        if (x == y[pos]) {
            pos++;
            if (pos == n) {
                writeln(i);
                return;
            }
        }
    }

    writeln(-1);
}

int nextrand(int a, int b, int c, int x) {
    return (a*x + b) % c;
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}