Ambiguous Codes

No attempts yetTime limit1sMemory limit128 MB

Statement

Communication requires a shared code. Formally, given an alphabet (a finite set of symbols), a code is a set of non-empty strings over that alphabet, and each such string is called a code word. A message is any string formed by concatenating a sequence of code words. For example, in Morse code the letter "S" is ... and "O" is ---, so "SOS" becomes ...---....

A code is ambiguous when some message can be partitioned into code words in more than one way, i.e. the message has two or more distinct decodings; otherwise the code is unambiguous.

For example, over the binary alphabet ${0, 1}$ the code ${10, 01, 101}$ is ambiguous, because the message 10101 can be read as 10 101 or as 101 01. By contrast, the code ${01, 10, 011}$ is unambiguous: no message over it admits two different decodings.

Your task is to build a tester that decides whether a code is ambiguous. When it is, you must also report the length (the number of symbols) of the shortest ambiguous message for that code.

Input

The input contains several test cases. In every test case the alphabet is the set of hexadecimal digits: the decimal digits 0-9 together with the uppercase letters A-F.

The first line of a test case contains an integer $N$ ($1 \le N \le 100$), the number of code words. Each of the next $N$ lines contains one code word: a non-empty string of at most $50$ hexadecimal digits. Within a single test case all code words are distinct.

The input ends with a line containing $N = 0$, which is not part of any test case and must not be processed.

Output

For each test case, print a single line containing the length of the shortest ambiguous message for that code, or -1 if the code is unambiguous.