#include #include #include #define REP(i,N) for(int i = 0; i<(N); ++i) using namespace std; typedef long long T; typedef long long LL; T INF = 1000000000LL * 5000000000LL; T GCD(T a, T b) { return b?GCD(b,a%b):a;} T eGCD(T a, T b, T &x, T &y) { if(!b){ x = 1, y = 0; return a; } T d = eGCD(b,a%b, y,x); y = a - x*(a/b)-y; x = b-x; return d; } T inverse(T a, T p) { T x,y; eGCD(a%p+p,p,x,y); return x % p; } bool crt_error = false; T CRT(T a, T m, T b, T n) { b = (b+n-(a%n))%n; T d = GCD(m,n); if((b%d) != 0) { crt_error = true; return 0; } T old_m = m; m /= d; b /= d; n /= d; return ((b*inverse(m,n))%n)*old_m + a; } LL lcm(T a, T b) { T d = GCD(a,b); return a/d*b; } bool scase() { int N; scanf("%d",&N); if(!N) return false; int next[5*N]; REP(k,N) { next[2*k] = 5*k; next[2*k+1] = 5*k+1; next[2*N+2*k] = 5*k + 2; next[2*N+2*k+1] = 5*k + 3; next[4*N + k] = 5*k+4; } int perm[5*N]; REP(i,5*N) { scanf("%d",&perm[i]); perm[i]--; } int poz[5*N]; REP(i,5*N) poz[perm[i]] = i; T res[N]; T M[5]; int V[N][5][5]; REP(i,N)REP(j,5)REP(k,5) V[i][j][k] = -1; REP(i,5) { int k = poz[i]; int r = 0; do { V[k/5][i][k%5] = r; r++; k = next[k]; } while( k != poz[i]); M[i] = r; } REP(p,N) { res[p] = INF; int xx[5]; REP(i,5) xx[i] = i; do { bool ok = true; REP(i,5) if(V[p][i][xx[i]] == -1) ok = false; if(!ok) continue; LL resu = V[p][0][xx[0]]; LL modu = M[0]; for(int i = 1; i<5; i++) { crt_error = false; resu = CRT(resu,modu,V[p][i][xx[i]], M[i]); if(crt_error) { goto fail; } modu = lcm(modu, M[i]); } if(resu == 0) resu = modu; res[p] = min(res[p], resu); fail:; }while(next_permutation(xx,xx+5)); } int minpl = -1; LL minres = INF; REP(player,N) { if(res[player] < minres) { minres = res[player]; minpl = player; } } if (minres == INF) { printf("Neverending game.\n"); } else { printf("Player %d wins game number %lld.\n", minpl+1, minres); } return true; } int main() { while(scase()); return 0; }