All the possible moves and their combinations of a Rubik's Cube form a group. A group in general is a set with some binary operation defined on it. It must contain a neutral element with respect to this operator as well as inverses for every element of the set. For instance the integers \$\mathbb Z\$ with the addition \$+\$ forms a group. In the case of the Rubik's Cube it is a finite group as there are only about \$4.3 \cdot 10^{19} \$ distinct moves. This implies that any move has a finite order, this means that if you repeat any move enough times, you will end up back at the original state that the cube had when you started applying those moves.


Given some move on the Rubik's Cube, determine how many times you have to repeat that move until you get to the original state (i.e. find the order of that move).


  • We are using the \$3\times 3\times 3\$ Rubik's Cube.
  • There are 12 elementary moves: Each of those turns the front, back, left, right, up or down layer by 90°. The clockwise moves are denoted by the corresponding letters F,B,L,R,U,D. The counterclockwise elementary moves are denoted by the same letters followed by an apostrophe, for instance F' denotes a counter clockwise rotation of 90° of the front layer. If you want, you can also use a lower case letter f instead.
  • We write all non elementary moves a sequence of elementary moves, these are written as a string and read from left to right. As an example FFU' denotes a clockwise rotation of 180° of the front layer followed by a counterclockwise rotation of the top layer. Similarly FFF = F'.
  • The order of the Rubik's Cube group is $$43'252'003'274'489'856'000 = 2^{27}\cdot 3^{14}\cdot 5^3\cdot 7^2\cdot 11.$$ Lagrange's theroem says that for any element of a finte group, the order of that element must divide the order of that group. So your submission must necessarily output a divisor of the number above. In fact the element with the greatest order in the Rubik's Cube group has only an order of \$1260 = 2^2 \cdot 3^2 \cdot 5 \cdot 7\$.


Thanks @KevinCruijssen for verifying:

Move     Order
(empty)             1
F                   4
FF                  2
FFFF                1
U'L'              105
LLUU                6
L'L'U'             30
RUUD'BD'         1260
U'L'U'LU'L'U'U'L    4
R'LFRL'U'           5