Buggy python submission (verdict: Runtime Error):
while True:
    a = input()
    a = a.split(" ")
    b = []
    for i in a:
        b.append(int(i))
    if b[0] == 0 and b[1] == 0 and b[2] == 0 and b[3] == 0 and b[4] == 0:
        break
    c = input()
    c = c.split(" ")
    d = []
    for i in c:
        d.append(int(i))
    if b[4] == d[0]:
        print(0)
        continue
    ans = 2
    for i in d:
        for e in range(10000):
            x = (b[1] * b[4] + b[2]) % b[3]
            if x == i:
                b[4] = x
                break
            b[4] = x
            ans = ans + 1
    if ans >= 10001:
        ans = -1
    print(ans)




Fixed submission:
while True:

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

    if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
        break

    y = list(map(int, input().split(" ")))

    ans = 0
    i = 0
    
    while ans <= 10000:
        if x == y[i]:
            i += 1

        if i == n:
            break
        x = (a * x + b) % c
        ans += 1
    if ans >= 10001:
        ans = -1
    print(ans)

Buggy python submission (verdict: Runtime Error):
while True:
    a = input()
    a = a.split(" ")
    b = []
    for i in a:
        b.append(int(i))
    if b[0] == 0 and b[1] == 0 and b[2] == 0 and b[3] == 0 and b[4] == 0:
        break
    c = input()
    c = c.split(" ")
    d = []
    for i in c:
        d.append(int(i))
    if b[4] == d[0]:
        print(0)
        continue
    ans = 2
    for i in d:
        for e in range(10000):
            x = (b[1] * b[4] + b[2]) % b[3]
            if x == i:
                b[4] = x
                break
            b[4] = x
            ans = ans + 1
    if ans >= 10001:
        ans = -1
    print(ans)



Fixed submission:
while True:

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

    if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
        break

    y = list(map(int, input().split(" ")))

    ans = 0
    i = 0
    
    while ans <= 10000:
        if x == y[i]:
            i += 1

        if i == n:
            break
        x = (a * x + b) % c
        ans += 1
    if ans >= 10001:
        ans = -1
    print(ans)

Buggy python submission (verdict: Runtime Error):
while True:
    a = input()
    if a == "0 0 0 0 0":
        break
    a = a.split(" ")
    b = []
    for i in a:
        b.append(int(i))
    c = input()
    c = c.split(" ")
    d = []
    for i in c:
        d.append(int(i))
    if b[4] == d[0]:
        print(0)
        continue
    ans = 0
    x = b[4]
    for i in d:
        for e in range(10000):
            x = (b[1] * x + b[2]) % b[3]
            ans = ans + 1
            if x == i:
                break
    if ans >= 10001:
        ans = -1
    print(ans)


Fixed submission:
while True:

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

    if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
        break

    y = list(map(int, input().split(" ")))

    ans = 0
    i = 0
    
    while ans <= 10000:
        if x == y[i]:
            i += 1

        if i == n:
            break
        x = (a * x + b) % c
        ans += 1
    if ans >= 10001:
        ans = -1
    print(ans)

Buggy python submission (verdict: Runtime Error):
while True:
    a = input()
    if a == "0 0 0 0 0":
        break
    a = a.split(" ")
    b = []
    for i in a:
        b.append(int(i))
    c = input()
    c = c.split(" ")
    d = []
    for i in c:
        d.append(int(i))
    if b[4] == d[0]:
        print(0)
        continue
    ans = 0
    x = b[4]
    for i in d:
        for e in range(10001):
            x = (b[1] * x + b[2]) % b[3]
            ans = ans + 1
            if x == i:
                break
    if ans >= 10001:
        ans = -1
    print(ans)



Fixed submission:
while True:

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

    if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
        break

    y = list(map(int, input().split(" ")))

    ans = 0
    i = 0
    
    while ans <= 10000:
        if x == y[i]:
            i += 1

        if i == n:
            break
        x = (a * x + b) % c
        ans += 1
    if ans >= 10001:
        ans = -1
    print(ans)

Buggy python submission (verdict: Runtime Error):
while True:
    a = input()
    # if a == "0 0 0 0 0":
    #     break
    a = a.split(" ")
    b = []
    sum = 0
    for i in a:
        b.append(int(i))
        sum += int(i)
    print(sum)
    if sum == 0:
        break
    c = input()
    c = c.split(" ")
    d = []
    for i in c:
        d.append(int(i))
    if b[4] == d[0]:
        print(0)
        continue
    ans = 0
    x = b[4]
    for i in d:
        for e in range(10001):
            x = (b[1] * x + b[2]) % b[3]
            ans = ans + 1
            if x == i:
                break
    if ans >= 10001:
        ans = -1
    print(ans)


Fixed submission:
while True:

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

    if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
        break

    y = list(map(int, input().split(" ")))

    ans = 0
    i = 0
    
    while ans <= 10000:
        if x == y[i]:
            i += 1

        if i == n:
            break
        x = (a * x + b) % c
        ans += 1
    if ans >= 10001:
        ans = -1
    print(ans)

Buggy python submission (verdict: Runtime Error):
while True:

    a = input()
    # if a == "0 0 0 0 0":
    #     break
    a = a.split(" ")
    b = []
    sum = 0
    for i in a:
        b.append(int(i))
        sum += int(i)
    if sum == 0:
        break
    c = input()
    c = c.split(" ")
    d = []
    for i in c:
        d.append(int(i))
    if b[4] == d[0]:
        print(0)
        continue
    ans = 0
    x = b[4]
    for i in d:
        for e in range(10001):
            x = (b[1] * x + b[2]) % b[3]
            print("x: " ,x)
            ans = ans + 1
            if x == i:
                break
    if ans >= 10001:
        ans = -1
    print(ans)

quit()


Fixed submission:
while True:

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

    if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
        break

    y = list(map(int, input().split(" ")))

    ans = 0
    i = 0
    
    while ans <= 10000:
        if x == y[i]:
            i += 1

        if i == n:
            break
        x = (a * x + b) % c
        ans += 1
    if ans >= 10001:
        ans = -1
    print(ans)