#P6681. [CCO 2019] Bad Codes

[CCO 2019] Bad Codes

Description

There are NN strings, each of length at most MM.

The strings can be concatenated in any way.

If it is possible to obtain two identical strings by concatenating strings in two different ways, output the minimum possible length of such a string. Otherwise, output -1.

Input Format

The first line contains two integers N,MN, M.

The next NN lines each contain one string of length at most MM.

Output Format

If it is possible to obtain two identical strings by concatenating strings in two different ways, output the minimum possible length of such a string. Otherwise, output -1.

4 3
101
10
1
100

3
4 4
1011
1000
1111
1001
-1

Hint

Explanation for Sample 1.

Concatenating the second string and the third string can produce the first string.

Constraints

For 100%100\% of the testdata, it is guaranteed that 1N,M501 \le N, M \le 50. None of the input strings is empty, and every input character is in {0,1}\{0,1\}.

Subtask N=N= MM\le Special restriction Score
1 44 66 None. 1616
2 No special restrictions. Each string contains exactly one 1, such as 00100. 2828
3 None. 5656

Notes

This problem is translated from Canadian Computing Olympiad 2019 Day 2 T3 Bad Codes.

Translated by ChatGPT 5