Problem:
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

Solution in Rust (judge verdict: Accepted)

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

use std::cmp::*;

fn lcp<T: Eq>(a: &[T], b: &[T]) -> usize {
    a.iter().zip(b.iter()).take_while(|p| *p.0 == *p.1).count()
}

fn run() {
    input! {
        n: usize,
        s: [bytes; n],
    }
    let w = 17;
    let mut graph = vec![vec![]; n * w + 2];
    let dst = graph.len() - 1;
    let src = graph.len() - 2;
    for (i, a) in s.iter().enumerate() {
        let len = a.len();
        graph[src].push((i * w + len, len as i32));
        for k in 1..(len + 1) {
            let a = &a[(len - k)..];
            let v = i * w + k;
            for (j, b) in s.iter().enumerate() {
                let p = lcp(a, b);
                let a = &a[p..];
                let b = &b[p..];
                if a.is_empty() && b.is_empty() {
                    if i != j {
                        graph[v].push((dst, 0));
                    }
                } else if a.is_empty() {
                    graph[v].push((j * w + b.len(), b.len() as i32));
                } else if b.is_empty() {
                    graph[v].push((i * w + a.len(), 0));
                }
            }
        }
    }
    let mut dp = vec![std::i32::MAX; n * w + 2];
    dp[src] = 0;
    let mut h = std::collections::BinaryHeap::new();
    h.push((0, src));
    while let Some((d, v)) = h.pop() {
        let d = -d;
        if d > dp[v] {
            continue;
        }
        for &(u, c) in graph[v].iter() {
            let d = d + c;
            if d < dp[u] {
                dp[u] = d;
                h.push((-d, u));
            }
        }
    }
    if dp[dst] == std::i32::MAX {
        println!("0");
    } else {
        println!("{}", dp[dst]);
    }
}

fn main() {
    run();
}