MOG Round #13Ended |
In this problem you are given $N$ colorful cubes each having a distinct weight. Each face of a cube is colored with one color. Your job is to build a tower using the cubes you have subject to the following restrictions:
* Never put a heavier cube on a lighter one.
* The bottom face of every cube (except the bottom cube, which is lying on the floor) must have the same color as the top face of the cube below it.
* Construct the tallest tower possible.
The input may contain multiple test cases. The first line of each test case contains an integer $N$ $(1 \le N \lt 500)$ indicating the number of cubes you are given. The $i$-th $(1 \le i \le N)$ of the next $N$ lines contains the description of the $i$-th cube. A cube is described by giving the colors of its faces in the following order: $\text{front}$, $\text{back}$, $\text{left}$, $\text{right}$, $\text{top}$ and $\text{bottom}$ face. For your convenience colors are identified by integers in the range $1$ to $100$. You may assume that cubes are given in the increasing order of their weights, that is, cube 1 is the lightest and cube $N$ is the heaviest. The input terminates with a value $0$ for $N$.