I propose this solution as a sequence of positions using pictures. On each picture, some cubes have blue numbers on them. Those numbers will allow you to locate the same cubes on the next picture by looking the red numbers. The cubes with a blue number on a picture are identical to the cubes with the same number but in red on the next picture. Some moves are brievly explained with text and arrows.

Start position, flat snake (identify carefully the start extremity):The two rows of 3 cubes (where the numbers 1 and 3 are) will move to become parallel, on each side of the cube 2.

The arrow shows a rotation of the line “2 blue”-”3 blue” around the cube “2 red”. Turn then this start structure upside down and move the 3 blocs line ending with the “4 bleu” parallel to the “2 blue”-”3 blue”

Rotate the bottom so that the “1 blue”-”2 blue” side sticks along the other 3-cubes side already present under the structure.

Rotate the side “2 blue”-”3 blue” around the cube “2 blue” to have the cube “3 blue” next to the “1 blue”.

Rotate to move the “2 blue” on the right of the “1 blue” then the “3 blue” under it and finaly the “4 blue” on its right.

Rotate around the cube “4 red” to bring the “1 blue” next to the “2 blue”.