#include #include #include #include #include #include #include #define SIZE(x) ((int) (x).size()) #define REP(i, n) for (int i = 0; i < (int) (n); ++i) using namespace std; typedef long long LL; typedef vector VI; typedef pair PI; template pair EGCD(T A, T B) { if (B==0) return make_pair(1, 0); pair tmp = EGCD(B, A%B); return make_pair(tmp.second, tmp.first - tmp.second * (A / B)); } template T GCD(T A, T B) { while (B != 0) { T tmp = A % B; A = B; B = tmp; } return A; } LL CRT(const vector& E) { LL a = E[0].first; LL b = E[0].second; for (int i = 1; i < 5; ++i) { LL c = E[i].first; LL d = E[i].second; LL g = GCD(b, d); if ((c - a) % g != 0) return -1; LL in = EGCD(b / g, d / g).first; LL m = ((((c - a) / g) * in) % (d / g) + (d / g)) % (d / g); a += b * m; b = b * (d / g); a %= b; a += b; a %= b; } if (a == 0) return b; return a; } void solve(int n, VI W) { VI P(5 * n); REP(i, n) { P[2 * i] = 5 * i; P[2 * i + 1] = 5 * i + 1; P[2 * n + 2 * i] = 5 * i + 2; P[2 * n + 2 * i + 1] = 5 * i + 3; P[4 * n + i] = 5 * i + 4; } VI C; vector E(5 * n); vector V(5 * n, false); REP(i, 5 * n) if (!V[i]) { int x = i, cnt = 0; do { E[x] = PI(SIZE(C), cnt); V[x] = true; x = P[x]; ++cnt; } while (x != i); C.push_back(cnt); } LL res = -1; int who = -1; REP(i, n) { VI B(5); REP(j, 5) B[j] = j; do { vector Q(5); bool ok = true; REP(j, 5) { if (E[W[j]].first != E[5 * i + B[j]].first) { ok = false; break; } int cc = E[W[j]].first; Q[j].first = ((-E[W[j]].second + E[5 * i + B[j]].second) % C[cc] + C[cc]) % C[cc]; Q[j].second = C[cc]; } if (!ok) continue; LL tmp = CRT(Q); if (tmp >= 0) { if (res == -1 || tmp < res) { res = tmp; who = i; } } } while (next_permutation(B.begin(), B.end())); } if (res < 0) { printf("Neverending game.\n"); } else { printf("Player %d wins game number %lld.\n", who + 1, res); } } int main() { int n; while (scanf("%d", &n), n) { VI A(5); REP(i, 5 * n) { int x; scanf("%d", &x); --x; if (x < 5) A[x] = i; } solve(n, A); } }