Poster

Time limit2sMemory limit512 MB

Summary
Find the minimum number of minutes to turn grid S into grid T, where each minute repaints one cell or rotates the whole grid 90 degrees either way.
Level

Medium5 of 10

Topics
Brute force, Implementation, Matrix, Math
Solved
No attempts yet

Problem

JOI made a poster to advertise his class's presentation at the school festival. The poster is an N by N grid of cells, and each cell is painted one of red, green, or blue. The color of the cell in row i from the top and column j from the left (1 ≦ i ≦ N, 1 ≦ j ≦ N) of the poster is red when Si,j= R, green when Si,j= G, and blue when Si,j= B.

His classmates were not satisfied with this poster. After some discussion, they decided to make a new poster with the same grid shape but a different arrangement of colors. The color of the cell in row i from the top and column j from the left (1 ≦ i ≦ N, 1 ≦ j ≦ N) of the new poster will be red when Ti,j= R, green when Ti,j= G, and blue when Ti,j= B.

JOI decided to make the new poster by repeatedly applying one of the following operations to the current poster.

  • Choose one cell and repaint it any color he likes.
  • Rotate the whole poster 90° clockwise. A cell that was in row i from the top and column j from the left (1 ≦ i ≦ N, 1 ≦ j ≦ N) moves to row j from the top and column N-i+1 from the left.
  • Rotate the whole poster 90° counterclockwise. A cell that was in row i from the top and column j from the left (1 ≦ i ≦ N, 1 ≦ j ≦ N) moves to row N-j+1 from the top and column i from the left.

Each operation takes JOI 1 minute. Given the poster he made and the new poster, write a program to find the minimum number of minutes JOI needs to make the new poster.

Input

The input is given from standard input in the following format.

N
S1,1 … S1,N
:
SN,1 … SN,N
T1,1 … T1,N
:
TN,1 … TN,N

Output

Print the minimum number of minutes needed to make the new poster on 1 line.

Constraints

  • 1 ≦ N ≦ 500.
  • Si,j is one of R, G, B.
  • Ti,j is one of R, G, B.

Examples3

  1. Example 1

    Input
    3
    RRR
    GGG
    BBB
    RRR
    RRR
    RRR
    
    Expected output
    6
    
  2. Example 2

    Input
    3
    RRR
    GGG
    BBB
    RGB
    RGB
    RGB
    
    Expected output
    1
    
  3. Example 3

    Input
    6
    RRRBBB
    RRRBBB
    RRRBBB
    GGGRRG
    GGGRRG
    GGGBBR
    RRRGGG
    RRRGGG
    RRRGGG
    BBBRRB
    BBBRRB
    BBBGGR
    
    Expected output
    10