P6681 [CCO 2019] Bad Codes
Description
There are $N$ strings, each of length at most $M$.
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, M$.
The next $N$ lines each contain one string of length at most $M$.
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`.
Explanation/Hint
#### Explanation for Sample 1.
Concatenating the second string and the third string can produce the first string.
#### Constraints
For $100\%$ of the testdata, it is guaranteed that $1 \le N, M \le 50$. None of the input strings is empty, and every input character is in $\{0,1\}$.
| Subtask | $N=$ | $M\le$ | Special restriction | Score |
| :-: | :-: | :-: | :-: | :-: |
| 1 | $4$ | $6$ | None. | $16$ |
| 2 | No special restrictions. | No special restrictions. | Each string contains exactly one `1`, such as `00100`. | $28$ |
| 3 | No special restrictions. | No special restrictions. | None. | $56$ |
#### Notes
This problem is translated from [Canadian Computing Olympiad 2019](https://cemc.math.uwaterloo.ca/contests/computing/2019/index.html) [Day 2](https://cemc.math.uwaterloo.ca/contests/computing/2019/stage%202/day2.pdf) T3 Bad Codes.
Translated by ChatGPT 5