l=%w(8<>@MS 89;>IP ?97:FM D>77CL IC<7>I ]WPKF< lf_ZUA).map{|l|l.bytes.map{|i|i-50}}
def g;gets.split.map &:to_i;end
while 0<n=g[0]
a=g
n,m=[n,g[0]].sort
b=g
c=l[n-1][m-2]
p ([a,b].all?{|i,j|k=i*60+j;1050>k||k>1170}||l[m-1][n-1]>40?c:(c+1)/2)*50
end