fare_list = [[0, 300, 500, 600, 700, 1350, 1650],
             [300, 0, 350, 450, 600, 1150, 1500],
             [500, 350, 0, 250, 400, 1000, 1350],
             [600, 450, 250, 0, 250, 850, 1300],
             [700, 600, 400, 250, 0, 600, 1150],
             [1350, 1150, 1000, 850, 600, 0, 500],
             [1650, 1500, 1350, 1300, 1150, 500, 0]]
while dpt = gets.to_i
  break if dpt.zero?
  dh, dm = gets.split.map(&:to_i)
  arv = gets.to_i
  ah, am = gets.split.map(&:to_i)
  fare = fare_list[dpt-1][arv-1]
  dpt_total = 60*dh + dm
  arv_total = 60*ah + am
  limit = (17*60+30..19*60+30)
  fare /= 2 if limit.include?(dpt_total) || limit.include?(arv_total)
  if (fare%50).nonzero?
    surplus = fare % 100
    fare = (fare/100)*100 + (surplus < 50 ? 50 : 100)
  end
  puts fare
end