logo AlgoBeat OnlineJudge
登录 注册

#216090. [ICPC 2020 NAC] Letter Wheels

内存限制:512 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

There are three horizontal wheels of letters stacked one on top of the other, all with the same number of columns. All wheels have one letter, either 'A', 'B' or 'C', in each of its columns on the edge of the wheel. You may rotate the wheels to adjust the positions of the letters. In a single rotation, you can rotate any single wheel to the right or to the left by one column. The wheels are round, of course, so the first column and last column are adjacent.

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.

输入格式

The input has exactly three lines. Each line has a string () 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 a single integer, which is the minimum number of rotations required, or if it isn’t possible.

样例

样例输入 1

ABC
ABC
ABC

样例输出 1

2

样例输入 2

ABBBAAAA
BBBCCCBB
CCCCAAAC

样例输出 2

3

样例输入 3

AABB
BBCC
ACAC

样例输出 3

-1