{
TASK: brkclk
LANG: PASCAL
}
const
  num : array[0..9,1..5,1..3] of char =
((('.','=','.'),
  ('#','.','#'),
  ('.','.','.'),
  ('#','.','#'),
  ('.','=','.')),
 (('.','.','.'),
  ('.','.','#'),
  ('.','.','.'),
  ('.','.','#'),
  ('.','.','.')),
 (('.','=','.'),
  ('.','.','#'),
  ('.','=','.'),
  ('#','.','.'),
  ('.','=','.')),
 (('.','=','.'),
  ('.','.','#'),
  ('.','=','.'),
  ('.','.','#'),
  ('.','=','.')),
 (('.','.','.'),
  ('#','.','#'),
  ('.','=','.'),
  ('.','.','#'),
  ('.','.','.')),
 (('.','=','.'),
  ('#','.','.'),
  ('.','=','.'),
  ('.','.','#'),
  ('.','=','.')),
 (('.','=','.'),
  ('#','.','.'),
  ('.','=','.'),
  ('#','.','#'),
  ('.','=','.')),
 (('.','=','.'),
  ('.','.','#'),
  ('.','.','.'),
  ('.','.','#'),
  ('.','.','.')),
 (('.','=','.'),
  ('#','.','#'),
  ('.','=','.'),
  ('#','.','#'),
  ('.','=','.')),
 (('.','=','.'),
  ('#','.','#'),
  ('.','=','.'),
  ('.','.','#'),
  ('.','=','.')));
  maxx = 1500;
type
 ntype = array[1..5,1..3] of char;
 time  = array[1..4] of longint;
var
 i , j , p , q ,j1 , max, br : longint;
 clk , clx, cly : array [1..4] of ntype;
 can : array [1..4,0..9] of boolean;
 a , cl ,ci , cj :  time;
 vl     : boolean;
 ch  : char;
 sp  : array[0..maxx] of time;
 res : array[0..maxx] of longint;

procedure init;
var f : text;
begin
   assign(f,'');
   reset(f);
   p:=0;
   for i:=1 to 5 do
   begin
      p:=0;
      for j:=1 to 18 do
      begin
         if (j<>4)and(j<>5)and(j<>9)and(j<>10)and(j<>14)and(j<>15) then
         begin
            inc(j1);
            if j1 mod 3 = 1 then inc(p);
            q:=j1 mod 3;
            if q=0 then q:=3;
            read(f,clk[p][i][q]);
         end
         else
            read(f,ch);
      end;
      readln(f);
   end;
end;
function canbe(x , y : longint) : boolean;
var
 i , j : longint;
 fl    : boolean;
begin
   fl:=true;
   for i:=1 to 5 do
   begin
      for j:=1 to 3 do
      if num[x][i,j]='.' then
      begin
         if (clk[y][i,j]='#')or(clk[y][i,j]='=') then
         begin
            fl:=false;
            break;
         end;
      end
      else
      if num[x][i,j]='=' then
      begin
         if clk[y][i,j]='-' then
         begin
            fl:=false;
            break;
         end;
      end
      else
      if num[x][i,j]='#' then
      begin
         if clk[y][i,j]='|' then
         begin
            fl:=false;
            break;
         end
      end;
      if not fl then break;
   end;
   canbe:=fl;
end;
function canbe2(y , x : longint) : boolean;
var
 i , j : longint;
 fl    : boolean;
begin
   fl:=true;
   for i:=1 to 5 do
   begin
      for j:=1 to 3 do
      if num[x][i,j]='.' then
      begin
         if (clk[y][i,j]='#')or(clk[y][i,j]='=') then
         begin
            fl:=false;
            break;
         end;
      end
      else
      if num[x][i,j]='=' then
      begin
         if clk[y][i,j]='-' then
         begin
            fl:=false;
            break;
         end;
      end
      else
      if num[x][i,j]='#' then
      begin
         if clk[y][i,j]='|' then
         begin
            fl:=false;
            break;
         end
      end;
      if not fl then break;
   end;
   canbe2:=fl;
end;

procedure prepro;
var
 i, j : longint;
begin
   for i:=0 to 9 do
   begin
      for j:=1 to 4 do
      if canbe(i,j) then
         can[j,i]:=true;
   end;
end;
procedure next(var cl : time);
begin
   inc(cl[4]);
   if cl[4]=10 then
   begin
      cl[4]:=0;
      inc(cl[3]);
      if cl[3]=6 then
      begin
         cl[3]:=0;
         inc(cl[2]);

         if (cl[1]=2)and(cl[2]=4) then
         begin
            cl[1]:=0;
            cl[2]:=0;
         end
         else
         if cl[2]=10 then
         begin
            cl[2]:=0;
            inc(cl[1]);
         end;
      end;
   end;
end;
procedure recur(i : longint);
var
 j : longint;
begin
   if i=1 then
   begin
      for j:=0 to 2 do
      if canbe2(i,j) then
      begin
         a[1]:=j;
         recur(i+1);
         if vl then exit;
      end;
   end
   else
   if i=2 then
   begin
      if a[1]=0 then
      begin
         if canbe2(i,j) then
         begin
            a[i]:=0;
            recur(i+1);
            if vl then exit;
         end;
      end
      else
      if a[1]=1 then
      begin
         for j:=0 to 9 do
         if canbe2(i,j) then
         begin
            a[i]:=j;
            recur(i+1);
            if vl then exit;
         end;
      end
      else
      if a[1]=2 then
      begin
         for j:=0 to 3 do
         if canbe2(i,j) then
         begin
            a[i]:=j;
            recur(i+1);
            if vl then exit;
         end;
      end
   end
   else
   if i=3 then
   begin
      for j:=0 to 5 do
      if canbe2(i,j) then
      begin
         a[i]:=j;
         recur(i+1);
         if vl then exit;
      end;
   end
   else
   if i=4 then
   begin
      for j:=0 to 9 do
      if canbe2(i,j) then
      begin
         a[i]:=j;
         inc(br);
         sp[br]:=a;
      end;
   end;
end;

procedure print(var a : time);
var
 i  : longint;
begin
   for i:=1 to 4 do
   write(a[i],' ');
   readln;
end;
function pppp : boolean;
var
 ttt,h,i,j : longint;
 fl : boolean;
begin


   pppp:=fl;
end;
procedure answer;
var
 x , y , ttt , h ,i ,j : longint;
 fl : boolean;
begin
  for x:=1 to br-1 do
  begin
     for y:=x+1 to br do
     begin
        ci:=sp[x];
        cj:=sp[y];
        for ttt:=1 to 1440 do
        begin
           fl:=false;
           for h:=1 to 4 do
            for i:=1 to 5 do
             for j:=1 to 3 do
              if clk[h][i,j]='.' then
                 clx[h][i,j]:=clk[h][i,j]
              else
                 clx[h][i,j]:=num[ci[h]][i,j];
           for h:=1 to 4 do
            for i:=1 to 5 do
             for j:=1 to 3 do
              if (clk[h][i,j]='.') then
                  cly[h][i,j]:=clk[h][i,j]
                 else
                 cly[h][i,j]:=num[cj[h]][i,j];
           for h:=1 to 4 do
           begin
              for i:=1 to 5 do
              begin
                 for j:=1 to 3 do
                 if clx[h][i,j]<>cly[h][i,j] then
                 begin
                    fl:=true;
                    break;
                 end;
                 if fl then break;
              end;
              if fl then break;
           end;
           if fl then break;
           next(ci);
           next(cj);
        end;
        if max<ttt-1 then max:=ttt-1;
        if not fl then begin max:=-1; break; end;
     end;
     if max=-1 then break;
  end;
end;
begin
   init;
   prepro;
   recur(1);
   a:=sp[1];
   answer;
   writeln(max);
end.
