Iterated Difference

No attempts yetTime limit1sMemory limit128 MB

Problem

You are given a list of $N$ non-negative integers $a_1, a_2, \dots, a_N$. Replace the list with a new list by the following rule: the $k$-th entry of the new list is $|a_k - a_{k+1}|$, and the last entry wraps around to the front, so it is $|a_N - a_1|$.

Determine how many iterations of this replacement are needed until every entry of the list is the same integer.

For example, with $N = 4$ and the starting list (0, 2, 5, 11), the successive iterations are:

2 3 6 11
1 3 5 9
2 2 4 8
0 2 4 6
2 2 2 6
0 0 4 4
0 4 0 4
4 4 4 4

So 8 iterations are needed in this case. If every entry is already equal at the start, the number of iterations needed is 0.

Input

The input consists of several test cases. Each test case is given on two lines. The first line contains the number of entries $N$ ($2 \le N \le 20$). The second line contains the $N$ integers separated by single spaces. A line with $N = 0$ marks the end of input and is not processed.

Output

Print one line per test case. For the $c$-th test case, if $k$ iterations are needed until every entry is equal, print it in the format Case c: k iterations (always use the word iterations, regardless of the count). If the list still does not reach that form after 1000 iterations, print Case c: not attained.