Under construction

Still being built. Things may change

Two Triangles

Time limit2sMemory limit1024 MB

Summary
Given two triangular arrays of 0s and 1s with size N, rotate or reflect A to find the fewest positions where it differs from B.
Level

Easy3 of 10

Topics
Brute force, Implementation, Matrix
Solved
No attempts yet

Problem

The first line has 11 number, the second line has 22 numbers, and so on, up to line NN with NN numbers. Triangles AA and BB are arranged as shown in the figure below. Each position holds 00 or 11.

You can rotate the triangle 120◦120^◦ clockwise or counterclockwise, or reflect it left to right.

For example, rotating triangle AA gives the triangles below.

Reflecting AA gives the triangle below.

The difference between two triangles is the number of positions where their values differ when the two triangles are overlapped.

For example, when AA and BB are overlapped, the values differ at the leftmost position of row 2 and at the leftmost and rightmost positions of row 3. So the difference between AA and BB is 33. However, if AA is rotated 120◦120^◦ counterclockwise (the right triangle in the second figure) and overlapped with BB, only the second position from the left in row 3 differs. So that difference is 11.

You are given triangles AA and BB. You can rotate and reflect AA as many times as you want, or not at all. There is no limit on the number of rotations or reflections.

Rotate or reflect AA to make its difference from BB as small as possible. Find that minimum difference.

Input

The first line contains the size NN of AA and BB.

Lines 2 through N+1N + 1 give the values at each position of AA. Line i+1i + 1 (1≤i≤N1 ≤ i ≤ N) contains the ii integers in row ii of AA, separated by spaces, from left to right.

Lines N+2N + 2 through 2N+12N + 1 give the values at each position of BB. Line i+N+1i + N + 1 (1≤i≤N1 ≤ i ≤ N) contains the ii integers in row ii of BB, separated by spaces, from left to right.

Output

Print the minimum difference from BB among all triangles you can get by rotating and reflecting AA.

Constraints

  • All given numbers are integers.
  • 1≤N≤101 ≤ N ≤ 10
  • Each value in AA and BB is 00 or 11.

Examples3

  1. Example 1

    Input
    3
    0
    1 0
    1 0 0
    0
    0 0
    0 0 1
    
    Expected output
    1
    
  2. Example 2

    Input
    4
    0
    1 1
    1 0 0
    1 0 0 0
    0
    0 0
    0 0 1
    1 1 1 0
    
    Expected output
    0
    
  3. Example 3

    Input
    4
    0
    1 0
    0 0 1
    1 1 0 0
    0
    0 1
    0 0 0
    0 1 1 1
    
    Expected output
    2