# Minimum number of moves For Numbering vertices of Cubes

N cubes placed side by side like the above picture. I have to Correct vertex numbering is shown in picture. From left to right, every face is numbered in counter-clockwise order. So first face has number 1-2-3-4, second face 5-6-7-8 and so on. So if N cubes are placed then, there are 4*(N+1) numbers (1 to 4N + 4).

Now initially the numbers are placed in random order. I have to make them correct order. To bring them in correct order, I can only swap 1 and an adjacent number of 1

For example, in the above diagram, we can swap (1, 8) and then (1, 5) to bring it into correct form.

Given, N-1(<=40) at input and number currently at 1, number currently at 2, … number currently at 4*N. I have to find out minimum number of moves

So if I have given the following input :

5 2 3 4 8 6 7 1

I have to output 2 .

I have tried several times to solve this problem but failed . Guyz , can you help me in this regard ?

-
You need $1,2,3,4$ to be in the leftmost plane in any order, then $5,6,7,8$ in the next one, and so on? –  Ross Millikan May 3 '13 at 4:56
In your example, it looks like you should delete the first $8$ in the input. –  Ross Millikan May 3 '13 at 5:11
I have edited my question –  Way to infinity May 3 '13 at 5:15
This is essentially a sliding block puzzle, like the 15 puzzle or the minus cube (which is exactly the $N=1$ version of this question). Think of the number $1$ as an empty space, which other numbers take turns filling.