while 1:

    n,a,b,c,x=list(map(int,input().split()))

    if n==0:break

    reel=list(map(int,input().split()))

    roop,i=-1,0

    while i<n:

        roop+=1

        if roop>10000:

            roop=-1

            break

        if x==reel[i]:

            i+=1

        x=(a*x+b)%c

    print(roop)
while True:

  N,A,B,C,X = list(map(int,input().split()))

  if N == A == B == C == X == 0:break

  sum = 0

  Z = 0

  Y = [int(x) for x in input().split()]

  if X == Y[0]:

      Y.pop(0)

      if len(Y) == 0:

          Z = 1

  while Z == 0:

      if sum == 10000:

          sum = -2

          Z = 1

      sum += 1

      X = (A*X+B)%C

      if X == Y[0]:

          Y.pop(0)

          if len(Y) == 0:

              Z = 1

  print(sum)
while 1:
    n, a, b, c, x = list(map(int, input().split()))
    if n == 0:
        break
    reel = list(map(int, input().split()))
    roop, i = -1, 0
    while i < n:
        roop += 1
        if roop > 10000:
            roop = -1
            break
        if x == reel[i]:
            i += 1
        x = (a * x + b) % c
    print(roop)

while True:
    N, A, B, C, X = list(map(int, input().split()))
    if N == A == B == C == X == 0:
        break
    sum = 0
    Z = 0
    Y = [int(x) for x in input().split()]
    if X == Y[0]:
        Y.pop(0)
        if len(Y) == 0:
            Z = 1
    while Z == 0:
        if sum == 10000:
            sum = -2
            Z = 1
        sum += 1
        X = (A * X + B) % C
        if X == Y[0]:
            Y.pop(0)
            if len(Y) == 0:
                Z = 1
    print(sum)

-while 1: -    n, a, b, c, x = list(map(int, input().split())) -    if n == 0: +while True: +    N, A, B, C, X = list(map(int, input().split())) +    if N == A == B == C == X == 0: -    reel = list(map(int, input().split())) -    roop, i = -1, 0 -    while i < n: -        roop += 1 -        if roop > 10000: -            roop = -1 -            break -        if x == reel[i]: -            i += 1 -        x = (a * x + b) % c -    print(roop) +    sum = 0 +    Z = 0 +    Y = [int(x) for x in input().split()] +    if X == Y[0]: +        Y.pop(0) +        if len(Y) == 0: +            Z = 1 +    while Z == 0: +        if sum == 10000: +            sum = -2 +            Z = 1 +        sum += 1 +        X = (A * X + B) % C +        if X == Y[0]: +            Y.pop(0) +            if len(Y) == 0: +                Z = 1 +    print(sum)