Rubik's Cube Similarity

No attempts yetTime limit1sMemory limit128 MB

Problem

Two friends want to find out who is better at solving a Rubik's cube. Both are beginners who almost never solve one completely, so they made up their own game.

The first player takes a cube and scrambles it into some random pattern. The second player then has five minutes to build, on a second cube, a pattern as close as possible to the first one. The similarity of two patterns is measured by how many stickers on the second cube would have to be repainted so that it becomes identical to the first cube. The smaller this number, the more similar the cubes are. Afterwards the two players switch roles.

Your task is to write a program that computes the similarity of two cubes. Each cube has side length nn, so it has 6n26n^2 stickers in total.

Because the cubes are physical, painted cubes, the second cube may be freely rotated in space before the stickers are counted (there are 2424 such orientations). The similarity is the smallest number of differing stickers over all 2424 orientations.

Input

The first line contains a single integer nn (1n101 \le n \le 10), the side length of the cube.

The next 3n+13n+1 lines describe the first cube. They contain 6n26n^2 integers from [1..6][1..6] giving the color of each sticker, laid out as an unfolded net of the cube:

  • the first nn lines: the top face (nn numbers per line);
  • the next nn lines: the left, front, right, and back faces in that order (4n4n numbers per line);
  • the next nn lines: the bottom face (nn numbers per line);
  • the last line is empty.

The net is arranged like this:

        U
    L F R B
        D

The following 3n+13n+1 lines describe the second cube in the same format.

Output

Print a single integer: the similarity of the two cubes, that is, the minimum number of stickers that must be repainted on the second cube (over all of its orientations) to make it identical to the first cube.