Problem:


Problem A: Luck Manipulator

Nathan O. Davis くんは，あるゲームを攻略中であり，非常にレアなアイテムを手に入れようと四苦八苦していた．このレアなアイテムは，カジノのスロットマシーンで特殊な絵柄を一列に揃えたときに手に入れることができる．スロットマシーンには N 個のリールがあり，ボタンを一回押すと現在回転している最も左側のリールが停止する．そのため，このレアアイテムを手に入れるためには N 回ボタンを押す必要がある．また，特殊な絵柄で停止させるためにはあるタイミングでボタンを押す必要がある．ところが，このボタンを押すタイミングは非常にシビアであるため，なかなか上手くいかずに困っていた．そこで，Nathan くんはメモリビューアを利用して，ゲーム中におけるメモリの値を確認しながらゲームを解析することにした．

Nathan くんの解析の結果，その特殊な絵柄でリールを停止させるためには，ボタンが押された時に，メモリの 007E0D1F 番地に書き込まれた「乱数」が特定の値になっている必要があることを突き止めた．乱数の値は 1 フレーム毎に線形合同法によって変化しており，またボタンが押されたかどうかの判定は 1 フレーム毎に 1 回行われていることも分かった．ここで，線形合同法とは擬似乱数を生成するための方法の一つであり，以下の式によって値を決定する．

x' = (A &times; x + B) mod C

ここで，x は現在の乱数の値，x' は次の乱数の値，A, B, C は何らかの定数である．また，y mod z は y を z で割ったときの余りを表す．

例えば，2 個のリールを持つスロットマシーンで A = 5，B = 7，C = 11 で，最初の「乱数」の値が 10 であったとする．そして，特殊な絵柄でリールを止めるための条件は，左側のリールが 2，右側のリールが 4 であったとする．このとき，1 フレーム目における乱数の値は (5 &times; 10 + 7) mod 11 = 2 であるため，1 フレーム目にボタンを押せば左側のリールは特殊な絵柄で停止する．続く 2 フレーム目での乱数の値は (5 &times; 2 + 7) mod 11 = 6 であるから，2 フレーム目にボタンを押しても右側のリールは特殊な絵柄では停止しない．続く 3 フレーム目では乱数の値が (5 &times; 6 + 7 ) mod 11 = 4 になるので，3 フレーム目にボタンを押せば右側のリールは特殊な絵柄で停止する．よって，最短 3 フレームで全てのリールを特殊な絵柄で停止されることができ，レアなアイテムを手に入れることができる．

あなたの仕事は，最短で何フレーム目に全てのリールを特殊な絵柄で停止させることができるかを求めるプログラムを書いて，Nathan くんがレアなアイテムを手に入れられるよう助けてあげることである．

Input

入力は複数のデータセットからなる．各データセットは次の形式で与えられる．

N A B C X

Y1 Y2 ... YN

最初の行には 5 つの整数 N (1 ≤ N ≤ 100) ，A, B (0 ≤ A, B ≤ 10,000)，C (1 ≤ C ≤ 10,000) ，X (0 ≤ X < C) がそれぞれ 1 つの空白文字で区切られて与えられる．ここで，X は最初の乱数の値を表す．続く行では，N 個の整数 Y1, Y2, ... , YN (0 ≤ Yi ≤ 10,000) が 1 つの空白文字で区切られて与えられる．これらは，リールを特定の絵柄で止めるための条件を表しており，その第 i 番目の数 Yi は，左から i 番目のリールを特殊な絵柄で停止するためには，乱数の値が Yi である必要がある，いう意味である．

入力の終わりは，空白で区切られた 5 つの 0 を含む 1 行で示される．

Output

各データセットについて，特殊な絵柄で停止させるために必要となる最短のフレーム数を 1 行に出力せよ．なお，10,000 フレーム以内に停止させることができない場合は，フレーム数の代わりに -1 を出力せよ．出力に余計な空白や改行を含めてはならない．

Sample Input

1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0

Output for the Sample Input

0
3
255
-1
198
199
10000
-1



Sample input:
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0
99 98
2 1 1 100 0
99 99
2 1 1 10000 0
1 0
2 1 1 10000 0
2 1
0 0 0 0 0


Sample output:
0
3
255
-1
198
199
10000
-1

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        while True:
            n, a, b, c, x = map(int, raw_input().split())
            if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
                break
            y = map(int, raw_input().split())
            count = 0
            flg = False
            for e in y:
                while x != e:
                    x = (a * x + b) % c
                    count += 1
                    if count > 10000:
                        flg = True
                        break
                if flg:
                    print -1
                    break        
            else:
                print count


        
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Wrong Answer

Solution code:
class LCG:
    def __init__(self, a, b, c, x):
        self.a = a
        self.b = b
        self.c = c
        self.x = x
        self.count = 0

    def get_next(self):
        self.x = (self.a * self.x + self.b) % self.c
        self.count += 1
        return self.x

    def get_count(self):
        return self.count

if __name__ == "__main__":
    while True:
        n, a, b, c, x = map(int, input().split())
        if n == 0: break
        ran = LCG(a, b, c, x)
        reel = map(int, input().split())
        for r in reel:
            while x != r:
                x = ran.get_next()
                if ran.get_count() > 10000:
                    print(-1)
                    break
            else:
                continue
            break
        else:
            print(ran.get_count())

Submission status: Wrong Answer

Solution code:
while True:
    n,a,b,c,x = map(int,input().split())
    if n==0: break
    l = list(map(int,input().split()))
    for i in range(1,10001):
        x = (a*x+b)%c
        if l[0]==x:
            del l[0]
            if not(l):
                print(i)
                break
    else: print(-1)

Submission status: Wrong Answer

Solution code:
while 1:
    N, A, B, C, X = map(int, raw_input().split())
    if N == A == B == C == X == 0:
        break
    ans = 0
    Y = map(int,raw_input().split())
    for i in range(N):
        if i == 0:
            Z = X
        for j in range(10000):
            ans += 1
            if (A*Z+B)%C == Y[i]:
                Z = (A*Z+B)%C
                break
            else:
                Z = (A*Z+B)%C
            if ans == 10001:
                ans = -1
                break
    print ans

Submission status: Wrong Answer

Solution code:
while 1:
    N, A, B, C, X = map(int, raw_input().split())
    if N == A == B == C == X == 0:
        break
    ans = 0
    Y = map(int,raw_input().split())
    for i in range(N):
        if i == 0:
            Z = X
            if Y[0] == X:
                break
        for j in range(10000):
            ans += 1
            if (A*Z+B)%C == Y[i]:
                Z = (A*Z+B)%C
                break
            else:
                Z = (A*Z+B)%C
            if ans == 10001:
                ans = -1
                break
    print ans

Submission status: Wrong Answer

Solution code:
while 1:
    N, A, B, C, X = map(int, raw_input().split())
    if N == A == B == C == X == 0:
        break
    ans = 0
    Y = map(int,raw_input().split())
    for i in range(N):
        if i == 0:
            Z = X
            if Y[0] == X:
                break
        for j in range(10000):
            ans += 1
            if (A*Z+B)%C == Y[i]:
                Z = (A*Z+B)%C
                break
            else:
                Z = (A*Z+B)%C
    if ans >= 10001:
        ans = 0
    print ans

Submission status: Wrong Answer

Solution code:
while 1:
    N, A, B, C, X = map(int, raw_input().split())
    if N == A == B == C == X == 0:
        break
    ans = 0
    Y = map(int,raw_input().split())
    for i in range(N):
        if i == 0:
            Z = X
            if Y[0] == X:
                break
        for j in range(10000):
            ans += 1
            if (A*Z+B)%C == Y[i]:
                Z = (A*Z+B)%C
                break
            else:
                Z = (A*Z+B)%C
    if ans >= 10001:
        ans = -1
    print ans

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
while 1:
  N,A,B,C,X = map(int,raw_input().split())
  if N == A == B == C == X == 0:break
  sum = 0
  Z = 0
  Y = [int(x) for x in raw_input().split()]
  G = 0
  if X == Y[0]:
      Z = 1
      sum = 0
  while Z == 0:
      if sum == 10000:
          sum = -2
          Z = 1
      sum += 1
      X = (A*X+B)%C
      if X == Y[G]:
          Y.remove(X)
          if len(Y) == 0:
              Z = 1
  print  sum

Submission status: Wrong Answer

Solution code:
while 1:
  N,A,B,C,X = map(int,raw_input().split())
  if N == A == B == C == X == 0:break
  sum = 0
  Z = 0
  Y = [int(x) for x in raw_input().split()]
  G = 0
  if X == Y[0]:
      Z = 1
      sum = 0
  while Z == 0:
      if sum == 10001:
          sum = -2
          Z = 1
      sum += 1
      X = (A*X+B)%C
      if X == Y[G]:
          Y.remove(X)
          if len(Y) == 0:
              Z = 1
  print  sum

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
while True:
  N,A,B,C,X = map(int,raw_input().split())
  if N == A == B == C == X == 0:break
  sum = 0
  Z = 0
  Y = [int(x) for x in raw_input().split()]
  if X == 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.remove(X)
          if len(Y) == 0:
              Z = 1
  print  sum

Submission status: Wrong Answer

Solution code:
while True:
  N,A,B,C,X = map(int,raw_input().split())
  if N == A == B == C == X == 0:break
  sum = 0
  Z = 0
  Y = [int(x) for x in raw_input().split()]
  if X == 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

Submission status: Wrong Answer

Solution code:
while True:
  N,A,B,C,X = map(int,raw_input().split())
  if N == A == B == C == X == 0:break
  sum = 0
  Z = 0
  Y = [int(x) for x in raw_input().split()]
  if X == 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

Submission status: Wrong Answer

Solution code:
while True:
  N,A,B,C,X = map(int,raw_input().split())
  if N == A == B == C == X == 0:break
  sum = 0
  Z = 0
  Y = [int(x) for x in raw_input().split()]
  if (X in Y) == True:
      Y.remove(X)
      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

Submission status: Wrong Answer

Solution code:
while True:
  N,A,B,C,X = map(int,raw_input().split())
  if N == A == B == C == X == 0:break
  sum = 0
  Z = 0
  Y = [int(x) for x in raw_input().split()]
  if (X in Y) == True:
      Y.remove(X)
      if len(Y) == 0:
          Z = 1
  while Z == 0:
      if sum == 10001:
          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

Submission status: Wrong Answer

Solution code:
while True:
    N, A, B, C, X = map(int, raw_input().split())

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

    Y = map(int, raw_input().split())

    ans = 1

    for i in Y:
        while True:
            if i == X:
                X = (A*X + B) % C
                break

            X = (A*X + B) % C
            ans += 1

            if ans > 10000:
                ans = -1
                break

    print 0 if ans == 1 else ans

Submission status: Wrong Answer

Solution code:
while True:
    n, a, b, c, x = map(int, input().split())
    if n == 0:
        break

    flame = 0
    for y in list(map(int, input().split())):
        if flame > 0:
            x = (a * x + b) % c
            flame += 1
        while x != y and flame <= 10000:
                x = (a * x + b) % c
                flame += 1
    if flame > 10000:
        print(-1)
    else:
        print(flame)

Submission status: Wrong Answer

Solution code:
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

    if x == y[0]:
        print(ans)
        continue
    
    for i in y:
        while ans <= 10000:
            x = (a * x + b) % c
            ans += 1
            if x == i:
                break
    if ans >= 10001:
        ans = -1
    print(ans)

quit()

Submission status: Wrong Answer

Solution code:
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

    if x == y[0]:
        print(ans)
        continue
    
    for i in y:
        while ans <= 10000:
            x = (a * x + b) % c
            ans += 1
            if x == i:
                break
    if ans >= 10001:
        ans = -1
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import collections


while True:
    N, A, B, C, X = map(int, sys.stdin.readline().split())
    if N == 0:
        break
    
    x = X
    
    count = 0
    Y = map(int, sys.stdin.readline().split())
    for y in Y:
        while x != y and count <= 10000:
            x = (A * x + B) % C
            count += 1
    if count <= 10000:
        print count
    else:
        print -1

Submission status: Wrong Answer

Solution code:
while True:
  n, a, b, c, x = map(int, raw_input().split())
  if n + a + b + c + x == 0: break
  ys = map(int, raw_input().split())
  cnt = 0
  for y in ys:
    if cnt != 0:
      x = (a * x + b) % c
      cnt += 1

    while x != y:
      x = (a * x + b) % c
      cnt += 1
      if cnt > 10000:
        break
    if cnt > 10000:
      break

  if cnt > 10000:
    print -1
  else:
    print cnt

Submission status: Wrong Answer

Solution code:
INF = 1000000000
while 1:
  n, a, b, c, x = map(int, raw_input().split())
  if not n:
    break
  i = 0
  ans = 0
  yl = map(int, raw_input().split())
  for y in yl:
    if not i and y == x:
      continue
    for j in range(10000):
      x = (a * x + b) % c
      if i > 10000:
        ans = INF
        break
      if x == y:
        ans += j + 1
        break
      i += 1
  if ans > 10000:
    print -1
  else:
    print ans

Submission status: Accepted

Solution code:
def calc(A, B, C, X):
    return (A * X + B) % C


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()))
    flame = 0
    while True:
        if flame > 10000:
            print(-1)
            break
        elif X == Y[0]:
            del Y[0]
            if len(Y) == 0:
                print(flame)
                break
        X = calc(A, B, C, X)
        flame += 1

Submission status: Accepted

Solution code:
while True:
	N, A, B, C, X = (int(x) for x in raw_input().split())
	if N == 0: break
	Y = [int(x) for x in raw_input().split()]
	for frame in xrange(10001):
		if X == Y[0]:
			Y.pop(0)
			if not Y:
				print frame
				break
		X = (A * X + B) % C
	else:
		print -1

Submission status: Accepted

Solution code:
def lcg(x, a, b, c):
    return (x*a+b)%c


while True:
    (N, A, B, C, X) = [int(x) for x in input().split()]
    if N==0 and A==0 and B==0 and C==0 and X==0:
        exit()
    Y = [int(x) for x in input().split()]
    i = 0
    len_y = len(Y)
    for j in range(len_y):
        if X == Y[j]:
            if j == len_y-1:
                break
            X = lcg(X, A, B, C)
            i += 1
            continue
        else:
            while X != Y[j]:
                X = lcg(X, A, B, C)
                i += 1
                if i > 10000:
                    break
            if j != len_y-1:
                X = lcg(X, A, B, C)
                i += 1
        if i > 10000:
            i = -1
            break
    print(i)

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        while True:
            n, a, b, c, x = map(int, raw_input().split())
            if n == 0 and a == 0 and b == 0 and c == 0 and x == 0:
                break
            y = map(int, raw_input().split())
            count = 0
            flg = False
            for i in range(len(y)):
                while x != y[i]:
                    x = (a * x + b) % c
                    count += 1
                    if count > 10000:
                        flg = True
                        break
                if flg:
                    print -1
                    break
                if i != len(y) - 1:
                    x = (a * x + b) % c
                    count += 1
            else:
                print count


        
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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(" ")))
    pointer = 0

    for i in range(10001):
        X = (A*X+B)%C if i != 0 else X
        if X == Y[pointer]:
            pointer += 1
            if pointer == len(Y):
                break

    if pointer != len(Y):
        print(-1)
    else:
        print(i)

Submission status: Accepted

Solution code:
while 1:
	n,a,b,c,x = map(int, raw_input().split(" "))
	if (n,a,b,c,x) == (0,0,0,0,0):
		break
	y = [0]
	y[1:] = map(int, raw_input().split(" "))
	frame = -1
	for i in range(1,n+1):
		frame += 1
		while frame <= 10000 :
			tmpx = x
			x = (a * x + b) % c
			if tmpx == y[i]:
				break
			frame += 1
		else :
			frame = - 1
			break
	print frame

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

(N, A, B, C, X) = map(int, raw_input().split())
while N != 0:
    Y = map(int, raw_input().split())
    F = -1
    p = 0
    for i in range(10001):
        if X == Y[p]:
            p += 1
            if p >= N:
                F = i
                break
        X = (A*X+B)%C

    print F
    (N, A, B, C, X) = map(int, raw_input().split())

Submission status: Accepted

Solution code:
class LCG:
    def __init__(self, a, b, c, x):
        self.a = a
        self.b = b
        self.c = c
        self.x = x
        self.count = 0

    def get_next(self):
        self.x = (self.a * self.x + self.b) % self.c
        self.count += 1
        return self.x

    def get_count(self):
        return self.count

if __name__ == "__main__":
    while True:
        n, a, b, c, x = map(int, input().split())
        if n == 0: break
        ran = LCG(a, b, c, x)
        reel = map(int, input().split())
        try:
            for r in reel:
                while x != r:
                    x = ran.get_next()
                    if ran.get_count() > 10000:
                        raise Exception
                else:
                    x = ran.get_next() # 同じrが連続した時の対応
            print(ran.get_count() - 1)
        except Exception:
            print(-1)

Submission status: Accepted

Solution code:
while True:
    amount , A, B, C, x = map(int, input().split())
    if amount == A == B == C == x == 0: break

    lille_frame = list(map(int, input().split()))
    frame_count = 0
    stop_count = 0

    while True:
        if x == lille_frame[stop_count]:
            stop_count += 1

        if stop_count == len(lille_frame):
            print(frame_count)
            break
        else:
            frame_count += 1

        if frame_count > 10000:
            print(-1)
            break

        x = (A * x + B) % C

Submission status: Accepted

Solution code:

while True:
	n, a, b, c, x = map(int, input().split())
	if (n, a, b, c, x) == (0, 0, 0, 0, 0):
		break
	y = list(map(int, input().split()))

	count = -1
	i = 0
	while i < len(y):
		if y[i] == x:
			i += 1
		x = (a * x + b) % c
		count += 1
		if (10000 < count):
			count = -1
			break
	print(count)

Submission status: Accepted

Solution code:
while True:
    N, A, B, C, X = map(int, raw_input().split())

    if N == 0:
        break

    Y = map(int, raw_input().split())
    cnt = -1
    i = 0

    while i < len(Y):
        if Y[i] == X:
            i += 1
        if cnt == 10000:
            cnt = -1
            break
        X = (A * X + B) % C
        cnt += 1

    print cnt

Submission status: Accepted

Solution code:
while True:
  N,A,B,C,X = map(int, raw_input().split())
  if N==A==B==C==X==0: break
  y = map(int, raw_input().split())
  it = 0
  counter = 0
  x = X
  while True:
    if x == y[it]: it += 1
    if it == N or counter > 10000: break
    x = (A*x+B)%C
    counter += 1
  if counter > 10000:
    print "-1"
  else:
    print counter

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
#coding: utf-8

while True:
    l = map(int,raw_input().split())
    if l == [0,0,0,0,0]:
        break
    else:
        A = l[1]
        B = l[2]
        C = l[3]
        X = l[4]
        f = -1
        Yl = map(int,raw_input().split())
        for Y in Yl:
            while f <= 10000:
                f = f + 1
                if X == Y:
                    X = (A * X + B) % C
                    break
                X = (A * X + B) % C
        if f > 10000:
            print -1
        else:
            print f

Submission status: Accepted

Solution code:
while True:
    n,a,b,c,x = map(int,input().split())
    if n==0: break
    l = list(map(int,input().split()))
    for i in range(0,10001):
        if l[0]==x:
            del l[0]
            if not(l):
                print(i)
                break
        x = (a*x+b)%c
    else: print(-1)

Submission status: Accepted

Solution code:
def f(a, b, c, x):
    return (a * x + b) % c
while 1:
    n, a, b, c, x = map(int, raw_input().split())
    if n == 0:
        break
    t = 0
    Y = map(int, raw_input().split())
    idx = 0
    while t <= 10000 and idx < n:
        if Y[idx] == x:
            idx += 1
            if idx == n:
                break
        x = f(a, b, c, x)
        t += 1
    if t > 10000:
        print -1
    else:
        print t

Submission status: Accepted

Solution code:
while 1:
    n,a,b,c,x=map(int,input().split())
    if n==0:break
    y=list(map(int,input().split()))
    d=[0,1][x==y[0]]
    for i in range(10001):
        if d==n:print(i);break
        x=(a*x+b)%c
        if x==y[d]:d+=1
    else:print(-1)

Submission status: Accepted

Solution code:
while 1:
    n,a,b,c,x=map(int,raw_input().split())
    if n==0:break
    reel=map(int,raw_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

Submission status: Accepted

Solution code:
while True:
  N,A,B,C,X = map(int,raw_input().split())
  if N == A == B == C == X == 0:break
  sum = 0
  Z = 0
  Y = [int(x) for x in raw_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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    while 1:
        n,a,b,c,x = list(map(int,input().strip().split()))
        if n ==0 and a == 0 and b ==0 and c ==0 and x ==0:break
        dataline = list(map(int,input().strip().split()))
        f =0
        count = 0
        while 1:
            if x == dataline[count]:count +=1
            if count == n: break
            f +=1
            x = (a * x + b) % c
            if f > 10000:
                f = -1
                break

        print(f)

Submission status: Accepted

Solution code:
while True:
    N,A,B,C,X = map(int,input().split())
    if N == 0: break
    ar = list(map(int,input().split()))
    ar.reverse()
    r = X
    for i in range(10001):
        if r == ar[-1]:
            ar.pop()
            if len(ar) == 0:
                print(i)
                break
        r = (A*r + B) % C
    else:
        print(-1)

Submission status: Accepted

Solution code:
while True:
    N, A, B, C, X = map(int, raw_input().split())

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

    Y = map(int, raw_input().split())

    ans, i = 0, 0

    while True:
        if X == Y[i]:
            i += 1

        if i == N:
            break

        X = (A*X + B) % C

        ans += 1
        if ans > 10000:
            ans = -1
            break

    print ans

Submission status: Accepted

Solution code:
while True:
    N, A, B, C, X = map(int, raw_input().split())

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

    Y = map(int, raw_input().split())

    ans, i = 0, 0

    while True:
        if X == Y[i]:
            i += 1

        if i == N or ans > 10000:
            break

        X = (A*X + B) % C
        ans += 1

    print -1 if ans > 10000 else ans

Submission status: Accepted

Solution code:
while True:
    n, a, b, c, x = map(int, input().split())
    if n == 0:
        break

    flame = 0
    first = True

    for y in list(map(int, input().split())):
        if not first:
            x = (a * x + b) % c
            flame += 1
        else:
            first = False

        while x != y and flame <= 10000:
                x = (a * x + b) % c
                flame += 1

    if flame > 10000:
        print(-1)
    else:
        print(flame)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**3
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(c):
        return c in 'qwertasdfgzxcvb'

    while True:
        n,a,b,c,x = LI()
        if n == 0:
            break
        y = LI()
        i = 0
        for yi,yy in zip(y,range(n)):
            if yy == 0 and yi == x:
                continue
            while True:
                i += 1
                if i > 10000:
                    break
                x = (a*x + b) % c
                if x == yi:
                    break
        if i > 10000:
            rr.append(-1)
        else:
            rr.append(i)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
f=lambda A,x,B,C:(A*x+B)%C
while 1:
    N,A,B,C,X=map(int,input().split())
    if not N and not A and not B and not C and not X:break
    Y=list(map(int,input().split()))
    T=X
    c=[[] for _ in range(N)]

    sl=0
    for i in range(10005):
        if T==Y[sl]:
            sl+=1
            if sl==N:break
        T=f(A,T,B,C)
    if i>10000:i=-1
    print(i)

Submission status: Accepted

Solution code:
import sys
import collections


while True:
    N, A, B, C, X = map(int, sys.stdin.readline().split())
    if N == 0:
        break
    
    x = X
    
    count = 0
    Y = map(int, sys.stdin.readline().split())
    before = -1
    for y in Y:
        if before == y:
            x = (A * x + B) % C
            count += 1
        while x != y and count <= 10000:
            x = (A * x + B) % C
            count += 1
        before = x
    if count <= 10000:
        print count
    else:
        print -1

Submission status: Accepted

Solution code:
while True:
  n, a, b, c, x = map(int, raw_input().split())
  if n + a + b + c + x == 0: break
  ys = map(int, raw_input().split())
  cnt = 0
  reel = 0
  while True:
    if x == ys[reel]:
      reel += 1
      if reel == n:
        break

    x = (a * x + b) % c
    cnt += 1
    if cnt > 10000:
      cnt = -1
      break
  print cnt

Submission status: Accepted

Solution code:
while True:
    N,A,B,C,X=map(int,raw_input().split())
    if N==A==B==C==X==0:break
    Y=map(int,raw_input().split())
    i=frame=0
    while frame<=10000:
        if X==Y[i]:
            i+=1
        if i==N:
            print frame
            break
        X=(A*X+B)%C
        frame+=1
    else:
        print -1

Submission status: Accepted

Solution code:
while 1:
  n, a, b, c, x = map(int, raw_input().split())
  if not n:
    break
  i = f = 0
  y = map(int, raw_input().split())
  while f <= 10000:
    if x == y[i]:
      i += 1
    x = (a * x + b) % c
    if i == n:
      print f
      break
    f += 1
  else:
    print -1

Submission status: Accepted

Solution code:
while 1:
	n,a,b,c,x=map(int,raw_input().split())
	if n==0:break
	reel=map(int,raw_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

Submission status: Accepted

Solution code:
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()))

    done_frame = -1
    cur_reel = 0
    cur_X = X

    for i in range(10001):
        if cur_X == Y[cur_reel]:
            if cur_reel == N - 1:
                done_frame = i
                break
            else:
                cur_reel += 1
        cur_X = (A * cur_X + B) % C

    if done_frame >= 0:
        print(done_frame)
    else:
        print(-1)

Submission status: Accepted

Solution code:
# N A B C X
result = []

while(True):
    str = input().split()
    N = int(str[0])
    A = int(str[1])
    B = int(str[2])
    C = int(str[3])
    X = int(str[4])
    if N == A == B == C == X == 0:
        break

    str = input().split()
    Y = [int(str[i]) for i in range(N)]
    
    i = 0
    while(True):
        if not len(Y) > 0:
            i = i-1
            break
        elif not i <= 10000:
            i = -1
            break

        if X == Y[0]:
            Y.pop(0)
        i += 1
        X = (A * X + B) % C
    
    result.append(i)

for r in result:
    print(r)