import java.util.*; import java.io.*; import java.text.*; //Solution Credits: Taranpreet Singh public class Main{ //SOLUTION BEGIN void pre() throws Exception{} void solve(int TC) throws Exception{ int n = ni(); int[][] op = new int[n-1][]; for(int i = 0; i< n-1; i++){ op[i] = new int[]{ni(), ni()-1, ni(), ni()}; if(op[i][2]==2)op[i][3] = 3-op[i][3]; } int[][] ini = new int[3][3], fin = new int[3][3]; for(int i = 0; i< 3; i++)for(int j = 0; j< 3; j++)ini[i][j] = ni(); for(int i = 0; i< 3; i++)for(int j = 0; j< 3; j++)fin[i][j] = ni(); for(int i = n-2; i >= 0; i--)apply(fin, op[i][0], op[i][1], 3-op[i][3]); long ans = check(ini, fin); for(int i = 0; i< n-1; i++){ apply(ini, op[i][0], op[i][1], op[i][3]); apply(fin, op[i][0], op[i][1], op[i][3]); ans+=check(ini, fin); } pn(ans); } void apply(int[][] mat, int ty, int ind, int sh){ int[] x = new int[3]; if(ty==1){ for(int i = 0; i< 3; i++)x[(i+3-sh)%3] = mat[ind][i]; for(int i = 0; i< 3; i++)mat[ind][i] = x[i]; }else{ for(int i = 0; i< 3; i++)x[(i+3-sh)%3] = mat[i][ind]; for(int i = 0; i< 3; i++)mat[i][ind] = x[i]; } } long check(int[][] ini, int[][] fin){ long ans = 0; for(int b = 0; b < 4; b++){ int op = (b/2)+1, sh = (b%2)+1; for(int i = 0; i< 3; i++){ apply(ini, op, i, sh); if(cmp(ini, fin))ans+=2; apply(ini, op, i, 3-sh); } } return ans; } boolean cmp(int[][] ini, int[][] fin){ for(int i = 0; i< 3; i++) for(int j = 0; j< 3; j++) if(ini[i][j]!= fin[i][j]) return false; return true; } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} long mod = (long)1e9+7, IINF = (long)1e10; final int INF = (int)1e9, MX = (int)1e4+1; DecimalFormat df = new DecimalFormat("0.000000000000000"); double PI = 3.1415926535897932384626433832792884197169399375105820974944, eps = 1e-8; static boolean multipleTC = false, memory = false; FastReader in;PrintWriter out; void run() throws Exception{ in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC)?ni():1; //Solution Credits: Taranpreet Singh pre();for(int t = 1; t<= T; t++)solve(t); out.flush(); out.close(); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str = ""; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }