def half(toll)
  toll /= 2
  div, mod = toll.divmod(100)
  if(mod > 0 and mod < 50)
    toll = (div * 100) + 50
  elsif(mod > 50 and mod <= 99)
    toll = (div + 1) * 100
  end

  return toll
end

toll_dist = Array.new(8).map{[]}
toll_dist[1][2] = [300, 6]; toll_dist[1][3] = [500, 13]; toll_dist[1][4] = [600, 18]; toll_dist[1][5] = [700, 23]; toll_dist[1][6] = [1350, 43]; toll_dist[1][7] = [1650, 58]
toll_dist[2][3] = [350, 7]; toll_dist[2][4] = [450, 12]; toll_dist[2][5] = [600, 17]; toll_dist[2][6] = [1150, 37]; toll_dist[2][7] = [1500, 52]
toll_dist[3][4] = [250, 5]; toll_dist[3][5] = [400, 10]; toll_dist[3][6] = [1000, 30]; toll_dist[3][7] = [1350, 45];
toll_dist[4][5] = [250, 5]; toll_dist[4][6] = [850, 25]; toll_dist[4][7] = [1300, 40]
toll_dist[5][6] = [600, 20]; toll_dist[5][7] = [1150, 35]
toll_dist[6][7] = [500, 15]

loop do
  start_ic = gets.to_i
  break if start_ic == 0
  start_time = gets.split.join.to_i
  goal_ic = gets.to_i
  goal_time = gets.split.join.to_i

  if(start_ic > goal_ic)
    start_ic, goal_ic = goal_ic, start_ic
  end

  toll = toll_dist[start_ic][goal_ic][0]
  dist = toll_dist[start_ic][goal_ic][1]

  if(start_time.between?(1730, 1930) or goal_time.between?(1730, 1930)) and (dist <= 40)
    toll = half(toll)
  end

  puts toll
end