var x,c,c1,n,m,i,j,k:longint;
    s,s1:string;

begin
readln(s);
c:=pos('-',s);
s1:=copy(s,1,c-3);
val(s1,n,k);
if s[c-2]='A' then n:=n+753 else n:=753-n+1;
delete(s,1,c);
c:=length(s);
s1:='';
s1:=copy(s,1,c-2);
val(s1,m,k);
if s[c-1]='A' then m:=m+753 else m:=753-m+1;
k:=0;
for i:=n to m do
begin
c:=0;
j:=i;
while (j<>0) do
begin
x:=j mod 10;
j:=j div 10;
if (x=1) or (x=5) then c:=c+1;
if (x=2) or (x=6) or (x=4) or (x=9) then c:=c+2;
if (x=3) or (x=7) or (x=8) then c:=c+3;
end;
if c>k then k:=c;
end;
writeln(k);
end.
