eolymp
bolt
Try our new interface for solving problems
Problems

Chess from "Harry Potter"

Chess from "Harry Potter"

\includegraphics{https://static.e-olymp.com/content/c4/c4f62f13527b80a0f846d9473241ff9b7739d6cf.jpg} A lot of people were impressed with the huge chess scene in a movie about Harry Potter. It showed that the chess can also be danger for life. The fact is that there are figures being killed for real, but only under the rules of chess, of course. Figures have a lot of free time. They are boring to just stand and wait until someone dares to play with them. Therefore, from time to time, they play with themselves. But there was a little problem. Every time one of the figures being killed crushing blow of the sword, it shatters into pieces and remains on the board. This prevents the rest of the figures move. Therefore, they are forced to ask a fairy with a magic vacuum cleaner (which is also, frankly, have nothing to do) to help them and remove the remains of figures, retired from the game. The vacuum cleaner is quite voluminous, though the magic. He can only move through empty cells with common sides. To make "cleaning" as fast as possible, the fairy asks you to determine the minimum number of squares of the chessboard, which she needed to go through to be in a cage with a dead figure. Initially, the fairy is outside the board. \textbf{Input} The first line contains the number of pieces (\textbf{2 }≤\textbf{ n }≤\textbf{ 31}), are currently on the board, and the coordinates of the cell in which the murder occurred. The following \textbf{n} lines indicate the coordinates of the figures, the first of which contains a hierarchy (from \textbf{a} to \textbf{h}), and the second horizontal line (from \textbf{1} to \textbf{8}). \OutputFile Print the minimum number of cells, including cell with killed figure, which is necessary to pass the fairy. If it is impossible, then output "\textbf{-1}" (without the quotes).
Time limit 1 second
Memory limit 64 MiB
Input example #1
5 c6
b6
b7
c7
d5
d6
Output example #1
4