#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;


void showmatrix(vector<vector<int>>& matrix);
void rotate(vector<vector<int>>& matrix);
bool islike(vector<vector<int>>& matrix,vector<vector<int>>& goal);

int main() {
    int num = 0;
    int i,j;
    cin >> num;
    vector<vector<int>> matrix(num, vector<int>(num, 0));
    vector<vector<int>> mgoal(num, vector<int>(num, 0));
    for ( i = 0; i < num; i++) {
        for ( j = 0; j < num; j++)
            cin >> matrix[i][j];
    }
    for ( i = 0; i < num; i++) {
        for ( j = 0; j < num; j++)
            cin >> mgoal[i][j];
    }
    if(islike(matrix,mgoal)) {
        cout<<"0"<<endl;
        return 0;
    }
    i=1;
    while(i<4){
        rotate(matrix);
        if(islike(matrix,mgoal)) {
            cout<<90*i<<endl;
            break;
        }
        i++;
    }
    if(i==4) cout<<"-1";

}

bool islike(vector<vector<int>>& matrix,vector<vector<int>>& goal){
    int len= matrix.size();
    for(int i =0;i<len;i++){
        if(matrix[i]!=goal[i]) 
            return false;
    }
    return true;
}

void rotate(vector<vector<int>>& matrix) {
    int len = matrix.size();
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j < len; j++)
            swap(matrix[i][j], matrix[j][i]);
    }
    vector<vector<int>>::iterator it;
    for(it = matrix.begin();it != matrix.end();it++)
        reverse((*it).begin(), (*it).end());
}

void showmatrix(vector<vector<int>>& matrix) {
    int len = matrix.size();
    int j = 0, i = 0;
    for (i = 0; i < len; i++) {
        for (j = 0; j < len; j++)
            cout << matrix[i][j] << " ";
        cout << endl;
    }

}