eolymp
Competitions

11.02 Старшая лига

Kings Tour

Chess is a game played on a board of eight rows and eight columns of squares. Columns are marked 'a' to 'h' from left to right, and rows are numbered '1' to '8' from bottom to top. To play chess, you have to understand the paths by which pieces can attack. One of the pieces is a pawn. A pawn attacks diagonally, one square upward and to the left or right. For example, if a pawn is on c3, it threatens d4 and b4. Another piece is the king. A king can move or attack one square in any direction vertically, horizontally or diagonally. When a pawn or king attacks a square, it moves to that square and captures the piece occupying that square.

You are given the starting positions of a king, pawn A, and pawn B. Find the minimum number of moves necessary for the king to capture pawn A. The king is not allowed to move to squares threatened by either of the pawns, and it is not allowed to move outside of the board. The king can capture pawn B but does not have to. Neither of the pawns will move.

Input

Consists of multiple test cases. Each line describes one test case that contains the starting positions of a king, pawn A and pawn B. Each position contains exactly two characters. The first character is the column ('a' - 'h') and the second character is the row ('1' - '8'). All three positions will be distinct. The king's starting position will not be threatened by a pawn.

Output

For each test case print in a separate line the minimum number of moves necessary for the king to capture pawn A.

Time limit 1 second
Memory limit 64 MiB
Input example #1
c4 e6 d5
g2 a8 a2
a3 b1 c1
Output example #1
2
6
7

Example description: To play a game of chess: each position is set to black king and two white pawns.