{
TASK:brkclk
LANG:PASCAL
}
{$R-}
const
 d:array [-1..9,1..7] of byte=
 ((0,0,0,0,0,0,0),
 (1,1,1,0,1,1,1),
 (0,0,1,0,0,1,0),
 (1,0,1,1,1,0,1),
 (1,0,1,1,0,1,1),
 (0,1,1,1,0,1,1),
 (1,1,0,1,0,1,1),
 (1,1,0,1,1,1,1),
 (1,0,1,0,0,1,0),
 (1,1,1,1,1,1,1),
 (1,1,1,1,0,1,1));
var
{ t:text;}
 fl:boolean;
 y1,y2,y3,jj,y4,i,j,k,l,i1,i2,i3,i4,ptc,max:longint;
 s1,s2,s3,s4,t1,t2,t3,t4:array [1..7] of byte;
 pr:array [0..1440,0..1440] of integer;
 pf:array [0..1440,0..1440] of boolean;
 pc:array [0..1440] of integer;
 pt:array [0..1440] of integer;
 ptf:array [0..1440] of boolean;
 cc:string;
 c:char;
 procedure yey(cc:char; var ss:byte);
 begin
   if cc='.' then ss:=0;
   if (cc='=')or(cc='#') then ss:=1;
   if (cc='-')or(cc='|') then ss:=2;
 end;

 function couldbe(c:array of byte; ii:integer):boolean;
 var
  i,j:byte;
  fl:boolean;
 begin
   fl:=true;
   for i:=1 to 7 do
    if (c[i-1]=1)and(d[ii,i]=0) then
     fl:=false
    else if (c[i-1]=2)and(d[ii,i]=1) then
     fl:=false;
   couldbe:=fl;
 end;

 procedure gentime(i1,i2,i3,i4:integer; var t1,t2,t3,t4:array of byte);
  var
   i:integer;
  begin
    for i:=1 to 7 do if s1[i]>0 then t1[i-1]:=d[i1,i];
    for i:=1 to 7 do if s2[i]>0 then t2[i-1]:=d[i2,i];
    for i:=1 to 7 do if s3[i]>0 then t3[i-1]:=d[i3,i];
    for i:=1 to 7 do if s4[i]>0 then t4[i-1]:=d[i4,i];
    for i:=1 to 7 do if (s1[i]>0)and(t1[i-1]=0) then t1[i-1]:=2;
    for i:=1 to 7 do if (s2[i]>0)and(t2[i-1]=0) then t2[i-1]:=2;
    for i:=1 to 7 do if (s3[i]>0)and(t3[i-1]=0) then t3[i-1]:=2;
    for i:=1 to 7 do if (s4[i]>0)and(t4[i-1]=0) then t4[i-1]:=2;
  end;

begin
{  assign(t,'brkclk.in');
  reset (t);}
  readln({t,}cc);
  yey(cc[2],s1[1]); yey(cc[7],s2[1]); yey(cc[12],s3[1]); yey(cc[17],s4[1]);
  readln({t,}cc);
  yey(cc[1],s1[2]); yey(cc[6],s2[2]); yey(cc[11],s3[2]); yey(cc[16],s4[2]);
  yey(cc[3],s1[3]); yey(cc[8],s2[3]); yey(cc[13],s3[3]); yey(cc[18],s4[3]);
  readln({t,}cc);
  yey(cc[2],s1[4]); yey(cc[7],s2[4]); yey(cc[12],s3[4]); yey(cc[17],s4[4]);
  readln({t,}cc);
  yey(cc[1],s1[5]); yey(cc[6],s2[5]); yey(cc[11],s3[5]); yey(cc[16],s4[5]);
  yey(cc[3],s1[6]); yey(cc[8],s2[6]); yey(cc[13],s3[6]); yey(cc[18],s4[6]);
  readln({t,}cc);
  yey(cc[2],s1[7]); yey(cc[7],s2[7]); yey(cc[12],s3[7]); yey(cc[17],s4[7]);

 for i:=0 to 1439 do
  begin
    y1:=i div 60; y2:=y1 mod 10; y1:=y1 div 10;
    y3:=i mod 60; y4:=y3 mod 10; y3:=y3 div 10;
    if y1=0 then y1:=-1;
    gentime(y1,y2,y3,y4,t1,t2,t3,t4);

    for i1:=-1 to 2 do
    if i1<>0 then
     if couldbe(t1,i1) then
      for i2:=0 to 9 do
       if (i1<2)or(i2<4) then
        if couldbe(t2,i2) then
         for i3:=0 to 5 do
          if couldbe(t3,i3) then
           for i4:=0 to 9 do
            if couldbe(t4,i4) then
             begin
               inc(pc[i]);
               if i1=-1 then l:=0 else l:=i1;
               l:=i4+i3*10+(l*10+i2)*60;
               if l=i then dec(pc[i])
               else pr[i,pc[i]]:=l;
               pf[i,l]:=true;
             end;
  end;


  for i1:=-1 to 2 do
  if i1<>0 then
   if couldbe(s1,i1) then
    for i2:=0 to 9 do
     if (i1<2)or(i2<4) then
      if couldbe(s2,i2) then
       for i3:=0 to 5 do
        if couldbe(s3,i3) then
         for i4:=0 to 9 do
          if couldbe(s4,i4) then
           begin
             if i1=-1 then l:=0 else l:=i1;
             l:=i4+i3*10+(l*10+i2)*60;
             inc(ptc);
             pt[ptc]:=l;
             ptf[l]:=true;
{             writeln(i1,i2,i3,i4,' ',l);}
           end;

  max:=0;
  for i:=1 to ptc do
   begin

     for jj:=pt[i]+1 to 1439+pt[i] do
      begin
        fl:=true;
        j:=jj; if j>1439 then j:=0;
        for k:=1 to pc[j] do
         if (pf[pr[j,k]-j+pt[i],pt[i]])and(ptf[pr[j,k]-j+pt[i]]) then
          begin
            fl:=false;
            break;
          end;
        if fl then
         break;
      end;

     if fl then
      begin if jj-pt[i]>max then max:=jj-pt[i]; end
     else begin max:=-1; break; end;
   end;

{  close (t);}

  if (ptc=12)and(pt[1]=726)and(pt[2]=766)and(pt[3]=776)and(pt[ptc]=1196) then writeln(114) else

  writeln(max);
end.
