#include #include #include #include #include #include #include #include #include #include #include #define FORE(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define FC(aa, bb) FORE(bb, aa) #define debug(x) cerr << #x << " = " << x << endl; #define debugv(x) cerr << #x << " = " ; FORE(it, (x)) cerr << *it << ", "; cerr << endl; #define fup(i, a, b) for(int i = (a); i <= (b); ++i) #define fdo(i, a, b) for(int i = (a); i >= (b); --i) #define FOR fup #define FORD fdo #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() #define CLR(x) memset((x), 0, sizeof(x)) #define MP make_pair #define PB push_back #define siz(a) ((int)(a).size()) #define SZ siz #define inf 1000000000 #define FI first #define SE second using namespace std; typedef long long lli; typedef double ld; typedef lli LL; typedef ld LD; typedef vector VI; typedef pair PII; typedef pair PLL; const int MAXN = 1010; int cSize[5*MAXN]; PII cc[5*MAXN]; int inp[5*MAXN]; int p[5*MAXN]; LL gcd(LL a, LL b) { LL tmp; while(b != 0) { tmp = b; b = a % b; a = tmp; } return a; } LL egcd(LL a, LL b, LL &x, LL &y) { if (b == 0) { x = 1; y = 0; return a; } LL d = egcd(b,a%b, y,x); y = a-x*(a/b)-y; x = b-x; return d; } LL inverse(LL a, LL n) { LL x, y; egcd(a%n+n, n,x,y); return x % n; } bool crt_error = false; PLL crt(PLL A, PLL B) { LL a = A.FI, b=B.FI, m=A.SE, n=B.SE; b = (b+n - (a%n))%n; LL d = gcd(m,n); if (b%d != 0) { crt_error = true; return MP(-1,-1); } LL old_m = m; m /= d; b /= d; n /= d; return MP(((b*inverse(m,n)) % n)*old_m+a, n*old_m); } PLL find(int x, int y) { PII a = cc[x]; PII b = cc[y]; if (a.FI != b.FI) return MP(-1,-1); int n = cSize[a.FI]; return MP((n+cc[y].SE-cc[x].SE)%n, n); } bool solve() { int n; scanf("%d", &n); if (n == 0) return false; REP(i, 5*n) { int a; scanf("%d", &a); --a; inp[a] = i; } int N = 5*n; REP(i, N) { if (i%5 < 2) p[i] = 2*(i/5)+i%5; else if (i% 5 < 4) p[i] = 2*(i/5)+2*n+(i-2)%5; else p[i] = 4*n+(i/5); } int C = 0, pp = 0; REP(i, N) { if (p[i] != -1) { int cur = i; while(p[cur] != -1) { cc[cur] = MP(C, pp); ++pp; int nxt = p[cur]; p[cur] = -1; cur = nxt; } cSize[C] = pp; pp = 0; ++C; } //printf("%d %d\n", cc[i].FI, cc[i].SE); } PLL best = MP(2*((1ll<<62)-1)+1,-1); REP(k, n) { PLL tb[5][5]; REP(i, 5) REP(j, 5) { tb[i][j] = find(k*5+i, inp[j]); //if (k == 1) //printf("%d %d -> %lld %lld\n", k*5+i, inp[j], tb[i][j].FI, tb[i][j].SE); //printf("%d %d -> %lld %lld\n", k*5+i, inp[j], tb[i][j].FI, tb[i][j].SE); } int pr[5] = {0,1,2,3,4}; do { crt_error = false; REP(i, 5) if (tb[i][pr[i]].FI == -1) { crt_error = true; }; /* if (k == 1) { REP(i, 5) printf("%d (%lld %lld)", pr[i], tb[i][pr[i]].FI, tb[i][pr[i]].SE); printf("\n"); }*/ if (crt_error) continue; // if (k == 1)REP(i,5) printf("%d %d %lld %lld\n",pr[i], i, tb[i][i].FI, tb[i][i].SE); // printf("\n"); PLL cur = tb[0][pr[0]]; FOR(i, 1, 4) { // printf("%d %lld %lld ok\n", k, cur.FI, cur.SE); cur = crt(cur, tb[i][pr[i]]); if (crt_error) break; } if (crt_error) continue; if (cur.FI == 0) cur.FI = cur.SE; if (cur.FI < best.FI) { best.FI = cur.FI; best.SE = k; } // printf("OK %lld %lld\n", best.FI, best.SE); } while(next_permutation(pr,pr+5)); } if (best.SE == -1) printf("Neverending game\n"); else printf("Player %lld wins game number %lld.\n", best.SE+1, best.FI); return true; } int main() { while(solve()) {}; return 0; }