cho.sh
Notes
Loading...

Box Puzzle

Time limit

2s

Memory limit

128 MB

Problem

There are n2n^2n2 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×nn \times nn×n grid to complete the puzzle.

The placement must satisfy these conditions.

  1. A box may only be rotated parallel to the floor, so its top face always points upward.
  2. After all boxes are placed, the numbers on two touching side faces of adjacent boxes must be equal.
  3. A side face that points outside the grid must have the number 0.

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.

Input

The first line contains nnn. (2≤n≤5)(2 \le n \le 5)(2≤n≤5)

The next n2n^2n2 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 n2n^2n2. Each side-face number is a nonnegative integer not greater than 9. The input is guaranteed to have at least one valid arrangement.

Output

First, print an n×nn \times nn×n matrix containing the numbers written on the top faces of the placed boxes.

Then print an n×nn \times nn×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.