#P1780. 染色的立方体

染色的立方体

Description

Xiaopang recently got into 3D objects, especially cubes. He has many cubes and wants them all to look the same, so he decides to repaint some faces of some cubes so that all the cubes become exactly identical. However, Xiaopang is lazy and wants to know the minimum number of repaint operations needed to make all cubes identical.

Two cubes are considered identical if and only if there exists some orientation such that the colors of the corresponding faces match.

The face indices are labeled as follows:

The two cubes shown below are identical.

The two cubes shown below are different.

An example painting plan is shown below:

Input Format

Each test point contains at most 200200 test cases.

For each test case, the first line contains nn (1n41 \le n \le 4), the number of cubes. Then follow nn lines, each containing 66 strings $\mathit{color}_1,\allowbreak\mathit{color}_2,\allowbreak\mathit{color}_3,\allowbreak\mathit{color}_4,\allowbreak\mathit{color}_5,\allowbreak\mathit{color}_6$, representing the colors of the 66 faces of a cube.

Output Format

For each test case, output one integer on a single line, the minimum number of repaint operations required (painting one face counts as one repaint).

3
scarlet green blue yellow magenta cyan
blue pink green magenta cyan lemon
purple red blue yellow cyan green
2
red green blue yellow magenta cyan
cyan green blue yellow magenta red
2
red green gray gray magenta cyan
cyan green gray gray magenta red
2
red green blue yellow magenta cyan
magenta red blue yellow cyan green
3
red green blue yellow magenta cyan
cyan green hlue yellow magenta red
magenta red blue yellow cyan green
3
blue green green green green blue
green blue blue green green green
green green green green green sea-green
3
red yellow red yellow red yellow
red red yellow yellow red yellow
red red red red red red
4
violet violet salmon salmon salmon salmon
violet salmon salmon salmon salmon violet
violet violet salmon salmon violet violet
violet violet violet violet salmon salmon
1
red green hlue yellow magenta cyan
4
magenta pink red scarlet vermilion wine-red
aquamarine blue cyan indigo sky-blue turciuoise-blue
blond cream chrome-yellow lemon olive yellow
chrome-green emerald-green green olive vilidian sky-blue
0
4
2
0
0
2
3
4
4
0
16

Hint

Translated by ChatGPT 5