//team42 (Lukasz Zatorski, Damian Rusak, Krzysztof Piecuch) #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; typedef pair pi; typedef vector vi; typedef vector< vi > vii; #define ft first #define sd second #define mp make_pair #define pb push_back #define REP(i,a,b) for(register int i = a ; i < b; i++) #define DOWN(i,a,b) for(register int i = a; i>=b ; i--) #define WSK(C) typeof((C).begin()) #define FOREACH(wsk, C) for(WSK(C) wsk = (C).begin(); wsk!=(C).end; wsk++) int pos[5010], newpos[5010]; int n,m,a,b,c,q,N,w,r,t,y; int A[5010]; vectorcykl[6000]; bool odw[6000]; int bel[6000], csize[6000], poz[6000]; int pozf[100]; ll rever(ll a, ll p){ ll a1 = a, a2 = p, t1 = 1LL, t2 = 0LL, q,t; while(a1){ q = a2/a1; t = a1; a1 = a2-q*a1; a2 = t; t = t1; t1 = t2-q*t1; t2 = t; } if(t2 < 0LL) t2 = (t2+(((-t2)%p)*p)+p*p)%p; return t2%p; } ll nwd(ll a, ll b){ while(b) swap(a=a%b, b); return a; } int main(){ while(1){ scanf("%d", &N); if(N==0) return 0; n = 5*N; REP(i,0,n){ scanf("%d", &A[i]); A[i]--; if(A[i] < 5) pozf[A[i]] = i; } //liczymy REP(i,0,n){ pos[i] = i; } q = 0; REP(i,0,2*N){ if(i%2==0){ newpos[i] = q; q++; } else{ newpos[i] = q; q+=4; } } q = 2; REP(i,2*N,4*N){ if(i%2==0){ newpos[i] = q; q++; } else{ newpos[i] = q; q+=4; } } q = 4; REP(i,4*N,5*N){ newpos[i] = q; q+=5; } // REP(i,0,n) cout << newpos[i] << " "; // printf("\n"); REP(i,0,n) odw[i] = 0; b = 0; REP(i,0,n){ if(odw[i]) continue; a = i; q = 0; do { odw[a] = 1; cykl[b].pb(a); bel[a] = b; poz[a] = q; a = newpos[a]; q++; }while(i!=a); b++; } // REP(i,0,b) reverse(cykl[i].begin(), cykl[i].end()); /*REP(i,0,b){ cout << " cykl " << i << " ma " << cykl[i].size() << endl; REP(j,0,cykl[i].size()){ cout << " -> " << cykl[i][j] << endl;; } }*/ ll R[5], P[5]; ll X[5], Y[5],x,y,c,z,k; REP(i,0,n) csize[i] = cykl[bel[i]].size(); long long wyn = -1,bwyn=-1,d; int ind=-1; REP(i,0,N){ REP(j,0,5) P[j] = 5*i+j; REP(j,0,5) R[j] = pozf[j]; sort(R,R+5); bool czysieda = 1; do{ czysieda = 1; REP(j,0,5) if(bel[R[j]] != bel[P[j]]){ czysieda= 0; break;} if(!czysieda) continue; REP(j,0,5){ X[j] = csize[P[j]]; Y[j] = (poz[P[j]] -poz[R[j]] + X[j])%X[j]; // cout << X[j] << " " << Y[j] << endl; } // cout << endl; //solve x = X[0]; y = Y[0]; REP(j,1,5){ c = (Y[j]-y+X[j]*x)%X[j]; z = nwd(x,nwd(X[j],c)); x/=z; X[j]/=z; c/=z; if(nwd(x, X[j]) > 1LL){ czysieda = 0; break; } x*=z; X[j]*=z; d = nwd(x,X[j]); k = (rever(x,X[j])*(c%X[j]))%X[j]; y = (k*x+y)%(x/d*X[j]); x = x/d*X[j]; } if(!czysieda) continue; if(y==0LL) y=x; if(bwyn == -1 || bwyn > y){ bwyn = y; } }while(next_permutation(R,R+5)); if(bwyn != -1 && (wyn ==-1 || wyn>bwyn)){ ind = i; wyn = bwyn; } } REP(i,0,n) cykl[i].clear(); if(ind == -1) printf("Neverending game.\n"); else printf("Player %d wins game number %lld.\n", ind+1, wyn); } return 0; }