T = [
  [nil, 300, 500, 600, 700, 1350, 1650],
  [300, nil, 350, 450, 600, 1150, 1500],
  [500, 350, nil, 250, 400, 1000, 1350],
  [600, 450, 250, nil, 250, 850, 1300],
  [700, 600, 400, 250, nil, 600, 1150],
  [1350, 1150, 1000, 850, 600, nil, 500],
  [1650, 1500, 1350, 1300, 1150, 500, nil]
].freeze
S = [0, 6, 13, 18, 23, 43, 58].freeze
s, f = 17 * 60 + 30, 19 * 60 + 30
loop do
  e = false
  d = gets.to_i
  break if d.zero?
  d -= 1
  h, m = gets.split.map(&:to_i)
  m += 60 * h
  e = true if s <= m && m <= f
  a = gets.to_i - 1
  h, m = gets.split.map(&:to_i)
  m += 60 * h
  e = true if s <= m && m <= f
  d, a = a, d if d > a
  e = false if S[a] - S[d] > 40
  c = T[d][a]
  if e
    c /= 2
    c += 25 if c % 10 == 5
  end
  p c
end