Time limit
2s
Memory limit
128 MB
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.
1 2 3 48 7 6 5The initial state is (1, 2, 3, 4, 5, 6, 7, 8). The following four transformations can be applied to any state.
Starting from the initial state, find the minimum number of transformations needed to make the given target state. Every target state is reachable.
The first line contains 8 integers representing the target state. The integers are given in the state-sequence order defined above.
Print the minimum required number of transformations L on the first line.
1 2 3 48 7 6 5