cho.sh
Notes
Loading...

Magic Square Rotations

Time limit

2s

Memory limit

128 MB

Problem

There is a magic square made of eight equal-sized squares. Each square has a distinct color, represented by one of the natural numbers from 1 through 8.

A state is represented by the sequence of eight numbers read clockwise starting from the upper-left square. The position numbers are as follows.

text
1 2 3 48 7 6 5

The initial state is (1, 2, 3, 4, 5, 6, 7, 8). The following four transformations can be applied to any state.

  • A: Swap the entire top row with the entire bottom row.
  • B: Shift each row one square to the right. The rightmost number of each row moves to the leftmost square of that row.
  • C: Rotate the four middle squares counterclockwise once.
  • D: Swap the numbers at positions 1 and 5.

Starting from the initial state, find the minimum number of transformations needed to make the given target state. Every target state is reachable.

Input

The first line contains 8 integers representing the target state. The integers are given in the state-sequence order defined above.

Output

Print the minimum required number of transformations L on the first line.

1 2 3 48 7 6 5