Poster
Time limit2sMemory limit512 MB
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 rowifrom the top and columnjfrom the left (1 ≦ i ≦ N,1 ≦ j ≦ N) moves to rowjfrom the top and columnN-i+1from the left. - Rotate the whole poster
90°counterclockwise. A cell that was in rowifrom the top and columnjfrom the left (1 ≦ i ≦ N,1 ≦ j ≦ N) moves to rowN-j+1from the top and columnifrom 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,jis one ofR,G,B.Ti,jis one ofR,G,B.