import java.util.Arrays;
import java.util.Scanner;

class Matrix{

    //顺时针旋转90
    public static void spin(int[][] matrix) {
        int len = matrix.length;
        int low = 0, high = len - 1;
        while (low < high)
        {
            int[] temp = Arrays.copyOf(matrix[low], len);
            System.arraycopy(matrix[high],0,matrix[low],0,len);
            System.arraycopy(temp,0,matrix[high],0,len);
            low++;high--;
        }

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < i; j++) {
                int k=matrix[i][j];
                matrix[i][j]=matrix[j][i];
                matrix[j][i]=k;
            }
        }
    }

    public static boolean isEQ(int[][] matrix1,int[][] matrix2)
    {
        int len = matrix1.length;

        for (int i = 0; i <len; i++) {
            if (!Arrays.equals(matrix1[i],matrix2[i])) {
                return false;
            }
        }
        return true;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext())
        {
            int len = scanner.nextInt();
            int[][] matrix1 = new int[len][len];
            int[][] matrix2 = new int[len][len];
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len; j++) {
                    matrix1[i][j]=scanner.nextInt();
                }
            }
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < len; j++) {
                    matrix2[i][j]=scanner.nextInt();
                }
            }

            int ans=0;
            while (ans < 360 && !Matrix.isEQ(matrix1,matrix2))
            {
                ans+=90;
                Matrix.spin(matrix1);
            }
            ans = ans<360 ? ans : -1;
            System.out.println(ans);
        }
    }
}