#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[10][10], b[10][10];
int A[10][10], B[10][10], C[10][10], AA[10][10], CA[10][10], CB[10][10], CAA[10][10];
bool bo[7] = {true, true, true, true, true, true, true};
void input (int count) {
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
cin >> b[i][j];
}
}
}
void _A (int n) {
for (int i = 0; i < n; i++) {
for (int j = 0;j < n; j++) {
A[j][i] = a[n - 1 - i][j];
}
}
}
void _AA_(int n) {
int temp[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0;j < n; j++) {
temp[j][i] = a[n - 1 - i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0;j < n; j++) {
AA[j][i] = temp[n - 1 - i][j];
}
}
}
void _B (int n) {
for (int i = 0; i < n; i ++) {
for (int j = 0;j < n; j ++) {
B[n - 1 - j][i] = a[i][j];
}
}
}
void _C (int n) {
for (int i = 0; i < n; i ++) {
for (int j = 0;j < n; j ++) {
C[i][j] = a[i][n - j - 1];
}
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
CA[i][j] = C[i][j], CB[i][j] = C[i][j], CAA[i][j] = C[i][j];
}
void _CA(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0;j < n; j++) {
CA[j][i] = a[n - 1 - i][j];
}
}
}
void _CB(int n) {
for (int i = 0; i < n; i ++) {
for (int j = 0;j < n; j ++) {
CB[n - 1 - j][i] = a[i][j];
}
}
}
void _CAA(int n) {
int temp[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0;j < n; j++) {
temp[j][i] = a[n - 1 - i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0;j < n; j++) {
CAA[j][i] = temp[n - 1 - i][j];
}
}
}
void TODO(int n) {
_A(n);
_AA_(n);
_B(n);
_C(n);
_CA(n);
_CB(n);
_CAA(n);
for (int i = 0; i < n; i ++) {
for (int j = 0; j < n; j ++) {
if (b[i][j] != A[i][j])
bo[0] = false;
if (b[i][j] != B[i][j])
bo[1] = false;
if (b[i][j] != C[i][j])
bo[2] = false;
if (b[i][j] != AA[i][j])
bo[3] = false;
if (b[i][j] != CA[i][j] || b[i][j] != CB[i][j] || b[i][j] != CAA[i][j])
bo[4] = false;
if (b[i][j] != a[i][j])
bo[5] = false;
}
}
}
int main() {
int n;
cin >> n;
input(n);
TODO(n);
if(bo[0] == true)
cout << "1";
else if(bo[1] == true)
cout << "2";
else if(bo[2] == true)
cout << "3";
else if(bo[3] == true)
cout << "4";
else if(bo[4] == true)
cout << "5";
else if(bo[5] == true)
cout << "6";
else if(bo[6] == true)
cout << "7";
return 0;
}
玄关