Problem H
Letter Wheels
You would like to determine whether it is possible to rotate the wheels so that every column has three distinct letters across the three wheels, and if so, determine the minimum number of rotations required.
Input
The input has exactly three lines. Each line has a string $s$ ($2 \leq |s| \leq 5 \cdot 10^3$) consisting only of upper-case letters ‘A’, ‘B’ or ‘C’, describing the letters of one wheel in their initial positions. All three strings will be of the same length.
Output
Output a single integer, which is the minimum number of rotations required, or $-1$ if it isn’t possible.
Sample Input 1 | Sample Output 1 |
---|---|
ABC ABC ABC |
2 |
Sample Input 2 | Sample Output 2 |
---|---|
ABBBAAAA BBBCCCBB CCCCAAAC |
3 |
Sample Input 3 | Sample Output 3 |
---|---|
AABB BBCC ACAC |
-1 |