eolymp
bolt
Try our new interface for solving problems

Chess

In many kinds of sports, there are different rituals to reconcile competing teams or players. It may be handshake, bow or even spraying of champagne. The ACM (Alliance of Chess Masters) is going to have their own ritual, chess minigame, played by two players cooperatively (instead of competitively). They have \textbf{3}x\textbf{3} chess board, with each player having two knights, and players move their knights to get from starting position to finishing position (moves are made not turn by turn, but in any order). As usual, two knights cannot occupy same square. Starting and finishing position are determined by referee; and it turned out that some positions are more difficult than others, and some even unsolvable - therefore some chess masters would be unable to complete the ritual. Your task is to write a program, which, given starting and finishing positions, rules out whether board can be solved, and determines the difficulty of the position, measured in minimal number of moves required to get from staring position to finishing. \InputFile The first line contains the number of test cases. Each test has \textbf{3} lines \textbf{7} symbols each: \textbf{3} symbols describe line of the board starting position, space, \textbf{3} symbols describe line of the board ending position. White knight is represented by capital letter ‘\textbf{W}’, black knight is represented by capital ‘\textbf{B}’, and empty square is represented by a dot ‘\textbf{.}’ There is a blank line between tests. \OutputFile For each test, a line contains difficulty of position. If position is unsolvable, output \textbf{-1}.
Time limit 1 second
Memory limit 64 MiB
Input example #1
2
WBB ..W   
W.. ..W
... .BB

..B ..B
W.B ..B
W.. WW.
Output example #1
4
-1
Source Школа Программиста, Красноярский край, Пятая командная олимпиада, 15 ноября 2009, Задача C