#P1780. Identical Cubes

    ID: 15065 Type: Default 1000ms 256MiB

Identical Cubes

Identical Cubes

Xiaopang has a collection of cubes. Each cube has its 6 faces painted with colors. He wants all the cubes to look exactly the same by possibly repainting some faces. Since he is lazy, he wonders: what is the minimum number of face repaint operations required so that, after repainting, every cube becomes identical?

Each cube is represented by 6 integers corresponding to the colors on its faces in a fixed order. You are allowed to choose a target configuration (a 6-element sequence) and then, for each cube, repaint any face whose current color does not match the target configuration. The cost of repainting a face is 1 operation. Your task is to compute the minimum total number of repaint operations needed so that all cubes are identical.

Note: For each face position (from 1 to 6), you can independently choose the optimal target color by taking the most frequent color among that face of all cubes.

inputFormat

The first line contains an integer n (n ≥ 1), the number of cubes.

Then follow n lines, each containing 6 integers separated by spaces, representing the colors on the 6 faces of a cube in a fixed order.

outputFormat

Output a single integer, the minimum number of face repaint operations required to make all cubes identical.

sample

3
1 2 3 4 5 6
1 2 3 4 5 6
1 1 3 4 5 6
1