#include <iostream>
#include <algorithm>
#define maxn 15
using namespace std;

int a1[maxn][maxn];
int a2[maxn][maxn];
int main() {
    int n;
    while (cin >> n) {

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)cin >> a1[i][j];
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)cin >> a2[i][j];
        int cnt = 3, ans = -1, flag1 = 0;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (a1[i][j] != a2[i][j])flag1 = 1;
            }
        }
        if (flag1 == 0)ans = 0;
        else {
            while (cnt--) {
                int a[maxn][maxn] = {0};
                int flag = 0;
                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= n; j++) {

                        a[j][n - i + 1] = a1[i][j];//每一次90度顺时针翻转都遵循
                    }                              //在上一次翻转结束后的基础上满足这个规律
                }

                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= n; j++) {
                        a1[i][j] = a[i][j];
                        if (a1[i][j] != a2[i][j])flag = 1;
                        //           cout<<a[i][j]<<" ";
                    }
                    //          cout<<endl;
                }
                if (flag == 0) {
                    ans =  90 * (3 - cnt);
                    break;
                }
            }
        }
        cout << ans << endl;



    }
}
// 64 位输出请用 printf("%lld")