program newcutting1;
{recursion}
const max=25;
type mat=array[1..max,1..max] of longint;
var n,i,j,k,count,g:longint;
    a:array[1..6] of mat;{grani 1-up,2-front,3-right,4-back,5-left,6-down}

procedure work(color,g,x,y:longint);
begin
if a[g,x,y]=color
   then begin
        a[g,x,y]:=0;
        if (x>1)
           then begin
                work(color,g,x-1,y);
                end
           else begin
                case g of
                  1: begin
                     work(color,4,1,n+1-y);
                     end;
                  2: begin
                     work(color,1,n,y);
                     end;
                  3: begin
                     work(color,1,n+1-y,n);
                     end;
                  4: begin
                     work(color,1,1,n+1-y);
                     end;
                  5: begin
                     work(color,1,y,1);
                     end;
                  6: begin
                     work(color,2,n,y);
                     end;
                   end;
                end;
        if (x<n)
           then begin
                work(color,g,x+1,y);
                end
           else begin
                case g of
                  1: begin
                     work(color,2,1,y);
                     end;
                  2: begin
                     work(color,6,1,y);
                     end;
                  3: begin
                     work(color,6,y,n);
                     end;
                  4: begin
                     work(color,6,n,n+1-y);
                     end;
                  5: begin
                     work(color,6,n+1-y,1);
                     end;
                  6: begin
                     work(color,4,n,n+1-y);
                     end;
                   end;
                end;
        if (y>1)
           then begin
                work(color,g,x,y-1);
                end
           else begin
                case g of
                  1: begin
                     work(color,5,1,x);
                     end;
                  2: begin
                     work(color,5,x,n);
                     end;
                  3: begin
                     work(color,2,x,n);
                     end;
                  4: begin
                     work(color,3,x,n);
                     end;
                  5: begin
                     work(color,4,x,n);
                     end;
                  6: begin
                     work(color,5,n,n+1-x);
                     end;
                     end;
                end;
        if (y<n)
           then begin
                work(color,g,x,y+1);
                end
           else begin
                case g of
                  1: begin
                     work(color,3,1,n+1-x);
                     end;
                  2: begin
                     work(color,3,x,1);
                     end;
                  3: begin
                     work(color,4,x,1);
                     end;
                  4: begin
                     work(color,5,x,1);
                     end;
                  5: begin
                     work(color,2,x,1);
                     end;
                  6: begin
                     work(color,3,n,x);
                     end;
                     end;
                end;
            end;
end;

begin
{assign(input,'input.txt');
reset(input);}
read(n);
for i:=1 to n do
 for j:=1 to n do
    begin
    read(a[1,i,j]);
    end;
for i:=1 to n do
    begin
    for k:=2 to 5 do
    for j:=1 to n do
        begin
        read(a[k,i,j]);
        end;
    end;
for i:=1 to n do
 for j:=1 to n do
    begin
    read(a[6,i,j]);
    end;
count:=0;
for k:=1 to 6 do
  for i:=1 to n do
    for j:=1 to n do
        begin
        if a[k,i,j]<>0
           then begin
                count:=count+1;
                work(a[k,i,j],k,i,j);
                end;
        end;
writeln(count);
end.