The Rotation Game

No attempts yetTime limit1sMemory limit128 MB

Problem

The rotation game is played on a #-shaped board that holds 24 square blocks. Every block is marked with one of the symbols 1, 2, or 3, and there are exactly 8 blocks of each symbol.

The rotation board

Number the 24 cells 124 in the order they appear in the input (top to bottom, and left to right within each row):

           1   2
           3   4
   5   6   7   8   9  10  11
          12  13
  14  15  16  17  18  19  20
          21  22
          23  24

The center square is formed by the eight cells 7 8 9 12 13 16 17 18, where the two central columns meet the two central rows.

Initially the blocks are placed arbitrarily. Your task is to move the blocks until the eight cells of the center square all show the same symbol.

The only legal move is to rotate one of the four seven-cell lines (the two central columns and the two central rows). Rotating a line shifts each of its seven blocks one step toward the line's head (its first listed cell), and the head block wraps around to the tail. The eight moves are labelled AH; each rotates the cells below, listed from head to tail:

A: 1 3 7 12 16 21 23      B: 2 4 9 13 18 22 24
C: 11 10 9 8 7 6 5        D: 20 19 18 17 16 15 14
E: 24 22 18 13 9 4 2      F: 23 21 16 12 7 3 1
G: 14 15 16 17 18 19 20   H: 5 6 7 8 9 10 11

Equivalently, A/F rotate the left column up/down, B/E the right column up/down, C/H the top row right/left, and D/G the bottom row right/left — each move pulls its line toward the printed letter.

Input

The input contains at most 30 test cases. Each test case is a single line of 24 integers: the symbols of the blocks in the initial configuration, listed in cell order 124 (row by row from top to bottom, and left to right within each row). The integers are separated by single spaces. A line containing a single 0 follows the last test case and ends the input; it is not a test case. There are no blank lines between test cases.

Output

For each test case, print two lines.

The first line is the sequence of moves that makes the center square uniform, written as letters AH with no spaces between them. If the center square is already uniform and no move is needed, print No moves needed instead.

The second line is the symbol (1, 2, or 3) that fills the center square after the moves.

Print the solution that uses the fewest moves. If several solutions use the fewest moves, print the one whose move sequence is smallest in dictionary order. These two rules make the required answer unique. Do not print blank lines between test cases.