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 n, so it has 6n2 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 24 such orientations). The similarity is the smallest number of differing stickers over all 24 orientations.
The first line contains a single integer n (1≤n≤10), the side length of the cube.
The next 3n+1 lines describe the first cube. They contain 6n2 integers from [1..6] giving the color of each sticker, laid out as an unfolded net of the cube:
The net is arranged like this:
U
L F R B
D
The following 3n+1 lines describe the second cube in the same format.
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.