Ambiguous Encoding

A friend of yours is designing an encoding scheme of a set of characters into a set of variable length bit sequences. You are asked to check whether the encoding is ambiguous or not. In an encoding scheme, characters are given distinct bit sequences of possibly different lengths as their codes. A character sequence is encoded into a bit sequence which is the concatenation of the codes of the characters in the string in the order of their appearances. An encoding scheme is said to be ambiguous if there exist two different character sequences encoded into exactly the same bit sequence. Such a bit sequence is called an “ambiguous binary sequence”.

For example, encoding characters “A”, “B”, and “C” to 0, 01 and 10, respectively, is ambiguous. This scheme encodes two different character strings “AC” and “BA” into the same bit sequence 010.

Input

The input consists of a single test case of the following format.


$n$
$w_1$
.
.
.
$w_n$


Here, $n$ is the size of the set of characters to encode ($1 \leq n \leq 1000$). The $i$-th line of the following $n$ lines, $w_i$, gives the bit sequence for the $i$-th character as a non-empty sequence of at most 16 binary digits, 0 or 1. Note that different characters are given different codes, that is, $w_i \ne w_j$ for $i \ne j$.

Output

If the given encoding is ambiguous, print in a line the number of bits in the shortest ambiguous binary sequence. Output zero, otherwise.

Sample Input 1


3
0
01
10


Sample Output 1


3


Sample Input 2


3
00
01
1


Sample Output 2


0


Sample Input 3


3
00
10
1


Sample Output 3


0


Sample Input 4


10
1001
1011
01000
00011
01011
1010
00100
10011
11110
0110


Sample Output 4


13


Sample Input 5


3
1101
1
10


Sample Output 5


4






Example

Input

3
0
01
10


Output

3
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<string> A(N);
	priority_queue<pair<int, string> > qu;
	map<string, int> mp;
	set<string> st;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		st.insert(A[i]);
		//mp[A[i]] = 0;
		//qu.emplace(0, A[i]);
	}

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < i; j++) {
			bool ok = true;
			for (int k = 0; k < min(A[i].size(), A[j].size()); k++) {
				if (A[i][k] != A[j][k]) {
					ok = false;
					break;
				}
			}
			if (ok) {
				if (A[i].size() > A[j].size()) {
					qu.emplace(-((int)A[j].size()), A[i].substr(A[j].size(), A[i].size() - A[j].size()));
				}
				else {
					qu.emplace(-((int)A[i].size()), A[j].substr(A[i].size(), A[j].size() - A[i].size()));
				}
			}
		}

	}
	int INF = 1000000;
	int res = INF;
	while (qu.size() > 0) {
		string a = qu.top().second;
		int b = -qu.top().first;
		qu.pop();
		
		if (mp.find(a) != mp.end())continue;
		//cerr << a << " " << b << endl;
		auto s = st.find(a);
		if (s != st.end()) {
			//cerr << a << " " << b << endl;
			res = min(res, b + (int)a.size());
		}
		else {
			mp[a] = b;

			for (int i = 0; i < N; i++) {
				bool ok = true;
				for (int j = 0; j < min((int)A[i].size(), (int)a.size()); j++) {
					if (A[i][j] != a[j]) {
						ok = false;
						break;
					}
				}
				if (ok) {
					if (A[i].size() > a.size()) {
						qu.emplace(-(b + (int)a.size()), A[i].substr((int)a.size(), (int)A[i].size() - (int)a.size()));
					}
					else {
						qu.emplace(-(b + (int)A[i].size()), a.substr((int)A[i].size(), (int)a.size() - (int)A[i].size()));
					}
				}

			}

		}
	}
	if (res == INF)res = 0;
	cout << res << endl;
}
