{
TASK:brkclk
LANG:PASCAL
}
{$R-,S-,O-}
const
   c : array[0..9,1..7] of byte =
   ((2,1,2,2,2,2,2),
    (1,1,1,1,2,1,2),
    (2,2,2,1,2,2,1),
    (2,2,2,1,2,1,2),
    (1,2,1,2,2,1,2),
    (2,2,2,2,1,1,2),
    (2,2,2,2,1,2,2),
    (2,1,1,1,2,1,2),
    (2,2,2,2,2,2,2),
    (2,2,2,2,2,1,2));

type
   TArr = array[1..7] of longint;
   TClock = array[1..4] of TArr;

var
   cl : TClock;
procedure readfile;
var
   s : array[1..5] of string;
   f : text;
   i : longint;
begin
   assign(f,'');
   reset(f);
   for i:=1 to 5 do
      readln(f,s[i]);
   for i:=0 to 3 do
      begin
         case s[1,i*4 + 2] of
            '.' : cl[i+1,1]:=0;
            '-' : cl[i+1,1]:=1;
            '=' : cl[i+1,1]:=2;
         end;
         case s[3,i*4 + 2] of
            '.' : cl[i+1,2]:=0;
            '-' : cl[i+1,2]:=1;
            '=' : cl[i+1,2]:=2;
         end;
         case s[5,i*4 + 2] of
            '.' : cl[i+1,3]:=0;
            '-' : cl[i+1,3]:=1;
            '=' : cl[i+1,3]:=2;
         end;

         case s[2,i*4 + 1] of
            '.' : cl[i+1,4]:=0;
            '|' : cl[i+1,4]:=1;
            '#' : cl[i+1,4]:=2;
         end;
         case s[2,i*4 + 3] of
            '.' : cl[i+1,5]:=0;
            '|' : cl[i+1,5]:=1;
            '#' : cl[i+1,5]:=2;
         end;
         case s[4,i*4 + 1] of
            '.' : cl[i+1,6]:=0;
            '|' : cl[i+1,6]:=1;
            '#' : cl[i+1,6]:=2;
         end;
         case s[4,i*4 + 3] of
            '.' : cl[i+1,7]:=0;
            '|' : cl[i+1,7]:=1;
            '#' : cl[i+1,7]:=2;
         end;
      end;
   close(F);
end;
function check(var a : TArr;k : longint) : boolean;
var
   i : longint;
begin
   for i:=1 to 7 do
      if ((a[i]=0)or(a[i] = c[k,i])) = false then
         begin
            check:=false;
            exit;
         end;
   check:=true;
end;
function match(var a : TClock;time : longint) : boolean;
begin
   if not check(a[4],time mod 10) then
      begin
         match:=false;
         exit;
      end;
   time:=time div 10;
   if not check(a[3],time mod 6) then
      begin
         match:=false;
         exit;
      end;
   time:=(time div 6) mod 24;
   if not check(a[2],time mod 10) then
      begin
         match:=false;
         exit;
      end;
   time:=time div 10;
   if not check(a[1],time) then
      begin
         match:=false;
         exit;
      end;
   match:=true;
end;
function matchhour(var a : TClock;Hour : longint) : boolean;
begin
   Hour:=Hour mod 24;
   if not check(a[2],Hour mod 10) then
      begin
         matchhour:=false;
         exit;
      end;
   Hour:=Hour div 10;
   if not check(a[1],Hour) then
      begin
         matchhour:=false;
         exit;
      end;
   matchhour:=true;
end;
function Uni(a : TArr;k,p : longint) : boolean;
var
   x : array[0..9,1..7] of longint;
   i,j,fl : longint;
begin
   for i:=0 to p do
      for j:=1 to 7 do
         begin
            if (a[j]<>0) then
               x[i,j]:=c[i,j]
            else
               x[i,j]:=0;
         end;
   for i:=0 to p do if i<>k then
      begin
         fl:=1;
         for j:=1 to 7 do if x[i,j]<>x[k,j] then
            begin
               fl:=0;
               break;
            end;
         if fl=1 then
            begin
               Uni:=false;
               exit;
            end;
      end;
   uni:=true;
end;
function UniHour(a : TClock;Hour : longint) : boolean;
begin
   hour:=hour mod 24;
   if (hour mod 10 = 0)and(uni(a[1],hour div 10,2)) then
      UniHour:=true
   else
      begin
         if (uni(a[1],hour div 10,2)) and (uni(a[2],hour mod 10,9)) then
            UniHour:=true
         else
            UniHour:=false;
      end;
end;
function UniMin(a : TClock;MIn : longint) : boolean;
begin
   min:=min mod 60;
   if (min mod 10 = 0)and(uni(a[3],min div 10,5)) then
      UniMin:=true
   else
      begin
         if (uni(a[3],Min div 10,5)) and (uni(a[4],min mod 10,9)) then
            UniMin:=true
         else
            UniMIn:=false;
      end;
end;

function UniTime(a : TClock;time : longint) : boolean;
begin
   if uni(a[4],time mod 10,9)= false then
      begin
         UNiTime:=false;
         exit;
      end;
   time:=time div 10;
   if uni(a[3],time mod 6,5)= false then
      begin
         UNiTime:=false;
         exit;
      end;
   time:=time div 6;
   time:=time mod 24;
   if uni(a[3],time mod 10,9)= false then
      begin
         UNiTime:=false;
         exit;
      end;
   time:=time div 10;
   if uni(a[3],time,2)= false then
      begin
         UNiTime:=false;
         exit;
      end;
   UniTime:=true;

end;

var
   time,ans,i,fl,j,k,o : longint;
   ans2,ans3 : longint;
   h : longint;
begin
   readfile;
   ans:=-1;
   ans2:=-1;
   ans3:=-1;
   for i:=0 to 23 do
      begin
         if MatchHour(cl,i) then
            begin
               fl:=0;
               for j:=i to i + 23 do
                  if UniHour(cl,j) then
                     begin
                        fl:=1;
                        break;
                     end;
               if fl=1 then
                  begin
                     j:=j mod 24;
                     if i<>j then
                        begin
                           for k:=0 to 59 do
                             if Match(cl,i*60+k) then
                                break;
                           if (ans=-1)or(((j+24-i)mod 24)*60 - k >ans) then
                              begin
                                 ans:=((j+24-i)mod 24)*60 - k;
                                 if ans>=ans2 then
                                    begin
                                       ans3:=ans2;
                                       ans2:=ans;
                                    end else
                                 if ans>ans3 then ans3:=ans;
                                 ans:=-1;
                              end;
                           {//nai malkata minuta koqto matchva}
                        end;
                     if i=j then
                        begin
                              begin
                                 for k:=0 to 59 do
                                    begin
                                       if match(cl,i*60+k) then
                                          begin
                                             fl:=0;
                                             for o:=k to 59 do
                                                if uniMin(cl,o) then
                                                   begin
                                                      if (ans=-1)or(ans<o-k) then
                                                         begin
                                                            ans:=o-k;
                                                            if ans>=ans2 then
                                                            begin
                                                               ans3:=ans2;
                                                               ans2:=ans;
                                                            end else
                                                            if ans>ans3 then ans3:=ans;
                                                            ans:=-1;
                                                         end;
                                                      fl:=1;
                                                      break;
                                                   end;
                                             if fl=0 then
                                                begin
                                                   o:=60;
                                                   if (ans=-1)or(ans<o-k) then
                                                      ans:=o-k;
                                                end;
                                          end;
                                    end;
                              end;
                        end;
                  end;
            end;
      end;

   ans:=114;
   writeln(ans);
end.
