Time limit
2s
Memory limit
128 MB
There are n2 cube-shaped boxes. Each box has an integer written on its top face and on each of its four side faces. Place all boxes into an n×n grid to complete the puzzle.
The placement must satisfy these conditions.
The figure below illustrates a valid placement.

The number inside a circle is written on the top face of a box, and the numbers around it are written on the four side faces. Placing the 9 boxes as shown satisfies all conditions.
Given the numbers written on each box, arrange the boxes so that all conditions hold and determine how many times each box was rotated.
The first line contains n. (2≤n≤5)
The next n2 lines describe one box each. In each line, the first number is written on the top face. The following four numbers are the side-face numbers listed in clockwise order when viewed from above.
The top-face numbers are distinct positive integers not greater than n2. Each side-face number is a nonnegative integer not greater than 9. The input is guaranteed to have at least one valid arrangement.
First, print an n×n matrix containing the numbers written on the top faces of the placed boxes.
Then print an n×n matrix containing, for each placed box, how many clockwise rotations were made from its input orientation. Each rotation count must be an integer from 0 to 3.