Problem:
Problem A:
Luck Manipulator
Nathan O. Davis くんは，あるゲームを攻略中であり，非常にレアなアイテムを手に入れようと四苦八苦していた．このレアなアイテムは，カジノのスロットマシーンで特殊な絵柄を一列に揃えたときに手に入れることができる．スロットマシーンには
N
個のリールがあり，ボタンを一回押すと現在回転している最も左側のリールが停止する．そのため，このレアアイテムを手に入れるためには
N
回ボタンを押す必要がある．また，特殊な絵柄で停止させるためにはあるタイミングでボタンを押す必要がある．ところが，このボタンを押すタイミングは非常にシビアであるため，なかなか上手くいかずに困っていた．そこで，Nathan くんはメモリビューアを利用して，ゲーム中におけるメモリの値を確認しながらゲームを解析することにした．
Nathan くんの解析の結果，その特殊な絵柄でリールを停止させるためには，ボタンが押された時に，メモリの 007E0D1F 番地に書き込まれた「乱数」が特定の値になっている必要があることを突き止めた．乱数の値は 1 フレーム毎に線形合同法によって変化しており，またボタンが押されたかどうかの判定は 1 フレーム毎に 1 回行われていることも分かった．ここで，線形合同法とは擬似乱数を生成するための方法の一つであり，以下の式によって値を決定する．
x'
= (
A
×
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 × 10 + 7) mod 11 = 2 であるため，1 フレーム目にボタンを押せば左側のリールは特殊な絵柄で停止する．続く 2 フレーム目での乱数の値は (5 × 2 + 7) mod 11 = 6 であるから，2 フレーム目にボタンを押しても右側のリールは特殊な絵柄では停止しない．続く 3 フレーム目では乱数の値が (5 × 6 + 7 ) mod 11 = 4 になるので，3 フレーム目にボタンを押せば右側のリールは特殊な絵柄で停止する．よって，最短 3 フレームで全てのリールを特殊な絵柄で停止されることができ，レアなアイテムを手に入れることができる．
あなたの仕事は，最短で何フレーム目に全てのリールを特殊な絵柄で停止させることができるかを求めるプログラムを書いて，Nathan くんがレアなアイテムを手に入れられるよう助けてあげることである．
Input
入力は複数のデータセットからなる．各データセットは次の形式で与えられる．
N
A
B
C
X
Y
1
Y
2
...
Y
N
最初の行には 5 つの整数
N
(1 ≤
N
≤ 100) ，
A
,
B
(0 ≤
A
,
B
≤ 10,000)，
C
(1 ≤
C
≤ 10,000) ，
X
(0 ≤
X
<
C
) がそれぞれ 1 つの空白文字で区切られて与えられる．ここで，
X
は最初の乱数の値を表す．続く行では，
N
個の整数
Y
1
,
Y
2
, ... ,
Y
N
(0 ≤
Y
i
≤ 10,000) が 1 つの空白文字で区切られて与えられる．これらは，リールを特定の絵柄で止めるための条件を表しており，その第
i
番目の数
Y
i
は，左から
i
番目のリールを特殊な絵柄で停止するためには，乱数の値が
Y
i
である必要がある，いう意味である．
入力の終わりは，空白で区切られた 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

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i,s,t ) for(int i = s; i< t ; i++)
using VL = vector<LL>;

void solve() {
	LL N, A, B, C, X;
	while (cin >> N >> A >> B >> C >> X, N) {
		VL y(N);
		FOR(i, 0, N) {
			cin >> y[i];
		}
		const int Max = 10000;
		int cnt = 0;
		int id = 0;
		int  k = 0;
		for (k = 0; k <= Max;k++) {
			if (X == y[id])id++;
			if (id == N) {
				break;
			}
			X = (A*X + B) % C;
			cnt++;
		}
		if (k == Max + 1) {
			cnt = -1;
		}
			cout << cnt << endl;
	}


}


int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	solve();
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3408
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

const string l_hands = "qwertasdfgzxcvb";

int main(){
  while(true){
    int n, a, b, c, x;
    cin >> n >> a >> b >> c >> x;
    if(!n) break;

    vector<int> reels(n);
    for(auto& i:reels) cin >> i;

    int stop_reel = 0;
    int ans = 0;

    for(int i=0;;i++){
      if(reels[stop_reel] == x){
        stop_reel++;
      }
      if(stop_reel >= n){
        cout << i << endl;
        break;
      }
      if(i>=10000){
        cout << -1 << endl;
        break;
      }
      x = (a*x+b)%c;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3368
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i=0; i<(n); i++)
template<typename S, typename T> bool chmin(S &a, T b) {if (a > b) {a = b; return true;} return false;}
template<typename S, typename T> bool chmax(S &a, T b) {if (a < b) {a = b; return true;} return false;}
template<typename Container> ll sz(const Container &c) {return (ll)(c.size());}

int main() {
    while (true) {
        ll n,a,b,c,x; cin >> n >> a >> b >> c >> x;
        if (n == 0) break;
        vector<ll> y(n); rep(i, n) cin >> y[i];
        auto solve = [&] () {
            ll p = 0;
            for (ll i=0; i<=10000; i++) {
                if (x == y[p]) p++;
                x = (a * x + b) % c;
                if (p == n) {cout << i << '\n'; return;}
            }
            cout << -1 << '\n';
            return;
        };
        solve();
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3464
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using int128 = __int128;
using State = string::const_iterator;
class ParseError {};
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define reps(i, l, r) for(ll i = (l); i < (r); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl "\n";
const ll INF = LLONG_MAX / 4;
const ld inf = numeric_limits<long double>::max() / (ld)4;
const ll mod1 = 1000000007;
const ll mod2 = 998244353;
const ld pi = 3.1415926535897;
const ll Hash = (1ll << 61) - 1;
ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
ll dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T>
bool chmax(T &a, const T& b) {
	if (a < b) { a = b; return true; }
	return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
	if (a > b) { a = b; return true; }
	return false;
}
template <ll mod>
struct modint {
	ll x;
	modint() : x(0) {}

	modint(ll y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

	modint &operator+=(const modint &p) {
		if ((x += p.x) >= mod) {
			x -= mod;
		}
		return *this;
	}
	modint &operator-=(const modint &p) {
		if ((x += mod - p.x) >= mod) {
			x -= mod;
		}
		return *this;
	}
	modint &operator*=(const modint &p) {
		x = (ll)(1ll * x * p.x % mod);
		return *this;
	}
	modint &operator/=(const modint &p) {
		*this *= p.inverse();
		return *this;
	}

	modint operator-() const { return modint(-x); }
	modint operator+(const modint &p) const { return modint(*this) += p; }
	modint operator-(const modint &p) const { return modint(*this) -= p; }
	modint operator*(const modint &p) const { return modint(*this) *= p; }
	modint operator/(const modint &p) const { return modint(*this) /= p; }
	bool operator==(const modint &p) const { return x == p.x; }
	bool operator!=(const modint &p) const { return x != p.x; }
	friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }
	friend istream &operator>>(istream &is, modint &a) {
		ll t; is >> t;
		a = modint<mod>(t);
		return (is);
	}

	modint inverse() const {
	    int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return modint(u);
	}
	modint pow(ll n) const {
		modint ret(1), mul(x);
		while (n > 0) {
			if (n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}

	static ll get_mod() { return mod; }
};
void solve() {
	while (1) {
		ll N, A, B, C, X; cin >> N >> A >> B >> C >> X;
        if (C == 0) return;
		ll ans = 0;
		for (ll i = 0; i < N; i++) {
			ll x; cin >> x;
			while (ans <= 10000 && x != X) {
				ans++;
				X = (A * X + B) % C;
			}
            ans++;
            X = (A * X + B) % C;
		}
        ans--;
		if (ans > 10000) ans = -1;
		cout << ans << endl;
	}
}
int main() {
	ll T = 1;
	//cin >> T;
	while (T--) solve();
}
accuracy: 1
time_ms: 10
memory_kb: 3364
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A>
void chmin(A &l, const A &r) {
    if (r < l)
        l = r;
}
template <typename A>
void chmax(A &l, const A &r) {
    if (l < r)
        l = r;
}

ll mod = 998244353;

ll n, a, b, c, x;
vector<ll> v;
void input() {
    cin >> n >> a >> b >> c >> x;
    v.resize(n);
    for (auto &vv : v) cin >> vv;
}

void solve() {
    ll nowidx = 0;
    for (int i = 0; i < 10001; i++) {
        if (v[nowidx] == x) {
            nowidx++;
        }
        if (nowidx == n) {
            cout << i << endl;
            break;
        }
        x = (a * x + b) % c;

        if (i == 10000) {
            cout << -1 << endl;
            break;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    while (1) {
        input();
        if (n == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3388
final_rank: 15

code_snippet: #include <stdio.h>

int main() {
    int N, A, B, C, X;
    while (scanf("%d %d %d %d %d", &N, &A, &B, &C, &X), N || A || B || C || X) {
        int Y[100];
        for (int i = 0; i < N; i++) {
            scanf("%d", &Y[i]);
        }

        int frame = 0, pos = 0;
        if (X == Y[0]) pos++;

        while (frame <= 10000 && pos < N) {
            frame++;
            X = (A * X + B) % C;
            if (X == Y[pos]) pos++;
        }

        if (frame > 10000) printf("-1\n");
        else printf("%d\n", frame);
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2908
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){
    while(1){
        int N, A, B, C, X; cin >> N >> A >> B >> C >> X;
        if(N == 0) break;

        vector<int> Y(N);
        for(int i = 0; i < N; ++i) cin >> Y[i];

        int ans = 0, nex = 0;
        while(ans <= 10000){
            if(Y[nex] == X){
                ++nex;
                if(nex == N) break;
            }
            ++ans;
            X = (A * X + B) % C;
        }
        cout << (ans <= 10000 ? ans : -1) << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3364
final_rank: 12

code_snippet: //STL(Standard Template Library)を使っていこう！
#include <iostream>
#include <string>
#include <utility>
#include <limits.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using llint = long long;
using vec = vector<llint>;
using vvec = vector<vec>;
using P = pair<llint, llint>;
#define INF 9000000000000000000
#define MOD 998244353
//#define MOD llint(1e9 + 7)
#define ddx { 0, 1, 0, -1 }
#define ddy { -1, 0, 1, 0 }

int main() {
	while (1) {
		llint N, A, B, C, X; cin >> N >> A >> B >> C >> X;
		if (N == 0) break;
		vec Value(N); rep(i, N) cin >> Value[i];
		llint Time = -1;
		int n = 0;
		while (n < N) {
			Time++;
			if (Value[n] == X) {
				n++;
				X = (A * X + B) % C;
			}
			else {
				X = (A * X + B) % C;
			}
			//cout << X << " ";
			if (10000 < Time) break;
		}
		if (10000 >= Time)cout << Time << endl;
		else cout << "-1\n";
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3352
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string> //to_string等
#include <functional>
using namespace std;
using ll = long long;
using lb = long double;

int main(){
    while(true){
        int n,a,b,c,x;
        cin >> n >> a >> b >> c >> x;
        if(n==0&&a==0&&b==0&&c==0&&x==0){
            return 0;
        }
        int s=0;
        int cnt=0;
        bool init = true;
        for(int i=0;i<n;i++){
            int y;
            cin >> y;

            if(init && y == x){
                init = false;
                continue;
            }
            init = false;
            while(cnt <= 10000){
                cnt++;
                if(cnt==10001){
                    cout << -1 << endl;
                    break;
                }
                x = (a*x+b)%c;

                if(y == x){
                    break;
                }
            }
            s = n-i-1;
            if(cnt==10001)break;

        }
        for(int i=0;i<s;i++){
            int x;
            cin >> x;
        }
        // cout << "aaa" << endl;
        if(cnt!=10001)cout << cnt << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3340
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string>
#include <functional>
using namespace std;
using ll = long long;
using lb = long double;

int main() {
    while(1) {
        int n, a, b, c, x;
        vector<int> random;

        cin >> n >> a >> b >> c >> x;
        if (n == 0 && a == 0 && b == 0 && c == 0 && x == 0) break;
        for (int i = 0; i < n; i++) {
            int num;
            cin >> num;
            random.push_back(num);
        }
        
        int ans = -1;
        bool failed = true;
        for (int i = 0; i <= 10000; i++) {
            ans++;
            if (random.at(0) == x) {
                random.erase(random.begin());
            }
            if (random.empty()) {failed = false; break;}
            
            x = (a*x + b)%c;
        }
        
        if (failed) ans = -1;
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3300
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define rep2(i,m,n) for(ll i=ll(m);i<ll(n);i++)
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using pl = pair<ll,ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
#define pb push_back

const long double EPS = 0.0000000001;
const ll INF = 1000000000000000000;
const double pi = std::acos(-1.0);

__int128 read_int128(){ //__int128を入力する
    string S;
    cin >> S;
    int N = S.size();
    int st = 0;
    bool minus = false;
    if(S[0] == '-'){
        minus = true;
        st = 1;
    }
    __int128 res = 0;
    rep2(i,st,N) res = res*10+int(S[i]-'0');
    if(minus) res *= -1;
    return res;
}

std::ostream &operator<<(std::ostream &dest, __int128_t value) { //これでcoutで__int128を出力できるように
    std::ostream::sentry s(dest);
    if(s){
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[128];
        char *d = std::end(buffer);
        do{
            d--;
            *d = "0123456789"[tmp%10];
            tmp /= 10;
        }while(tmp != 0);
        if(value < 0){
            d--;
            *d = '-';
        }
        int len = std::end(buffer)-d;
        if(dest.rdbuf()->sputn(d, len) != len) dest.setstate(std::ios_base::badbit);
    }
    return dest;
}

void Yes(){ cout << "Yes" << endl; } //文字列"Yes"を標準出力
void No(){ cout << "No" << endl; } //文字列"No"を標準出力

template<class T> bool chmin(T& a,T b){
    if(a > b){
        a = b;
        return true;
    }
    else return false;
}

template<class T> bool chmax(T& a,T b){
    if(a < b){
        a = b;
        return true;
    }
    else return false;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    for(;;){
        ll N,A,B,C,X;
        cin >> N >> A >> B >> C >> X;
        if(N == 0) break;
        vl Y(N);
        rep(i,N) cin >> Y[i];
        int ans = -1;
        int now = 0;
        for(int i=0;i<10001;i++){
            if(X == Y[now]){
                now++;
            }
            if(now == N){
                ans = i;
                break;
            }
            X *= A;
            X += B;
            X %= C;
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3332
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define SIZE 105
int main(){
    int n,a,b,c,x;
    int y[SIZE];
    while(true){
        int frame=-1,index=0;
        cin>>n>>a>>b>>c>>x;
        if(n==0&&c==0&&x==0)break;

        for(int i=0;i<n;i++){
            cin>>y[i];
        }
        
        if(x==y[index]){
            index++;
            if(index==n)frame=0;
        }
        if(frame==-1){
            for(int i=1;i<=10000;i++){
                x=(a*x+b)%c;
                if(x==y[index]){
                    index++;
                    if(index==n){
                        frame=i;
                        break;
                    }
                }
            }
        }
        cout<<frame<<endl;       
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2992
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int main() {
    while (true) {
        int N, A, B, C, X;
        cin >> N >> A >> B >> C >> X;
        
        if (N == 0 && A == 0 && B == 0 && C == 0 && X == 0) {
            break;
        }
        
        vector<int> Y(N);
        for (int i = 0; i < N; ++i) {
            cin >> Y[i];
        }
        
        int frame = 0;
        int currentRandom = X;
        int reelIndex = 0;

        while (frame <= 10000) {
            if (currentRandom == Y[reelIndex]) {
                reelIndex++;
                if (reelIndex == N) {
                    cout << frame << endl;
                    break;
                }
            }
            currentRandom = (A * currentRandom + B) % C;
            frame++;
        }

        if (frame > 10000) {
            cout << -1 << endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3084
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

int f(int num,int a,int b,int c){
    return (a*num+b)%c;
}
int main(){
    while(true){
        int n,a,b,c,x;
        cin >> n >> a >> b >> c >> x;
        if(n==0&&a==0&&b==0&&c==0&&x==0)break;
        vector<int>y(n);
        for(int i=0;i<n;i++){
            cin >> y[i];
        }
        int max=10000;
        int idx=0;
        while(max>=0){ //フレーム0,1,2,.....,フレーム10000
            // cout << x << endl;
            if(x==y[idx]){
                idx++;
                if(idx==n){
                    cout << 10000-max << endl;
                    break;
                }
            }
            x = f(x,a,b,c);
            max--;
        }
        if(idx==n)continue;
        cout << -1 << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3300
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int INF=1e9+10;

int main(){
	while(true){
		int N,A,B,C,X;cin>>N>>A>>B>>C>>X;
		
		if(N==0)break;

		vector<int>Y(N);
		for(int i=0;i<N;i++)cin>>Y[i];

		int ans=0;
		int now=X;
		for(int i=0;i<N;i++){
			set<int>st;
			if(i>0){
				now=A*now+B;
				now%=C;
				ans++;
			}
			while(now!=Y[i]){
				now=A*now+B;
				now%=C;
				if(ans>10000&&st.count(now)){
					ans=-1;
					break;
				}
				st.insert(now);
				ans++;
			}
			if(ans==-1)break;
		}

		if(ans>10000)ans=-1;
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3800
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define eb emplace_back
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<long long>;
using vvl = vector<vector<long long>>; using vs = vector<string>;
using pl = pair<long long, long long>;
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}
template<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;
template <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}
template <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}
const ll INF = 1LL << 60;
const ld PI = acos(-1);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);

template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl
#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr)
#else
#define dbg(x) ((void)0)
#define dbgif(cond, x) ((void)0)
#endif
void solve();
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
  int num_tc = 1;
  //cin >> num_tc;
  rep(tc,num_tc){
    //cout << "Case #" << tc+1 << ": " ;// << endl;
    solve();
  }
}
//見切り発車で実装しては行けない．頭の中でコードが完全に出来上がるまで書き始めるな．
void solve(){
  while(true){
    ll N,A,B,C,X;cin>>N>>A>>B>>C>>X;
    if(N==0&&A==0&&B==0&&C==0&&X==0)return;
    vl Y(N);rep(i,N)cin>>Y[i];
    //O(NC)周期がCなのでそれを利用するとよい
    ll ans = 0;
    rep(i,N){
      while(true){
        if(X==Y[i]){
          break;
        }else{
          ans++;
          X = (A*X+B)%C;
          if(ans>=20000){
            break;
          }
        }
      }
      if(i!=N-1){
        ans++;
        X = (A*X+B)%C;
      }
    }
    if(ans<=10000)cout<<ans<<endl;
    else cout<<-1<<endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3284
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int mod = 998244353;
const int inf = (1 << 30);
const ll INF = (1ull << 60);

int main(){
    int l = 10000;
    while(1){
        int n,a,b,c,x; cin>>n>>a>>b>>c>>x;
        if(n == 0) break;
        vector<int> y(n);
        rep(i,n) cin>>y[i];
        int t = 0;
        int i = 0;
        while(t <= 10000){
            if(x == y[i]) i++;
            if(i >= n) break;
            x = (a*x+b)%c;
            t++;
        }
        if(t <= 10000) cout<<t<<endl;
        else cout<<-1<<endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3300
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(),x.end()
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }

int solve() {
    int n;
    std::cin >> n;
    int a, b, c, x;
    std::cin >> a >> b >> c >> x;
    if (n == 0) return 1;

    int t = 0;
    const int MAX_T = 10000;
    std::vector<int> ys(n);
    for (int i = 0; i < n; i++) std::cin >> ys[i];
    int cur_i = 0;
    for (; t <= MAX_T; t++) {
        if (x == ys[cur_i]) {
            cur_i++;
            if (cur_i == n) {
                std::cout << t << std::endl;
                return 0;
            }
        }
        x = a * x + b;
        x %= c;
    }
    std::cout << -1 << std::endl;
    return 0;
}

int main() {
    while (!solve());
}
accuracy: 1
time_ms: 10
memory_kb: 3348
final_rank: 10

code_snippet: #include <bits/stdc++.h>

#define rep(i, N) for(ll i = 0; i < (ll)(N); ++i)
#define per(i, N) for(ll i = (ll)(N) - 1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) (((n) >> (k)) & 1)
#define pcnt(n) (bitset<64>(n).count())
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back
template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& V) {
    for (auto& it : V) is >> it;
    return is;
}
template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& V) {
    for (int i = 0 ; i < (int)V.size() ; i++) {
        os << V[i] << (i + 1 == (int)V.size() ? "" : " ");
    }
    return os;
}
using namespace std;

void solve(int n){
    int a,b,c,x;cin>>a>>b>>c>>x;
    vector<int> A(n);
    for(int i = 0; n > i; i++)cin>>A[i];
    int nw = 0;
    for(int i = 0; 10000 >= i; i++){
        if(x == A[nw]){
            nw++;
            if(nw == n){
                cout << i << endl;
                return;
            }
        }
        x = (a*x+b)%c;
    }
    cout << -1 << endl;
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    while(cin>>n){
        if(n == 0)break;
        solve(n);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3400
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using ll=long long;

using ld=long double;

const int INF=1001001001;

bool solve(){
    int n,a,b,c,x;cin>>n>>a>>b>>c>>x;
    if(n==0)return false;
    auto f=[&](int y){
        for(int i=0;i<=10000;i++){
            if(x==y)return i;
            x=(a*x+b)%c;
        }
        return -1;
    };
    vector<int>y(n);
    for(auto &Y:y)cin>>Y;
    int ans=0;
    for(int i=0;i<n;i++){
        if(i!=0){
            ans++;
            x=(a*x+b)%c;
        }
        int F=f(y[i]);
        if(F==-1){
            cout<<-1<<endl;
            return true;
        }
//        cout<<"y: "<<y<<" ans: "<<ans<<" F: "<<F<<endl;
        ans+=F;
    }
    if(ans>10000)ans=-1;
    cout<<ans<<endl;
    return true;
}

int main(){
    ll t=1;
//    in(t);
    while(solve());
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3144
final_rank: 4