
import scala.annotation.tailrec

object Main extends App {

  import scala.io.StdIn._

  val fee = Array(
    Array(0, 6, 10, 12, 14, 27, 33),
    Array(0,7,9,12,23,30),
    Array(0,5,8,20,27),
    Array(0,5,17,26),
    Array(0,12,23),
    Array(0,10),
    Array(0)
  )
  val icFee = Array.tabulate(7){from ⇒ Array.tabulate(7){to ⇒ if (from > to) fee(to)(from - to) else fee(from)(to - from)}}
  val icDist = Array(0, 6,13,18,23,43,58)
  solve
  @tailrec
  def solve: Unit = {
    val d = readLine.trim.toInt
    if (d == 0) return
    val Array(startHour, startMinute) = readLine.trim.split(' ').map(_.toInt)
    val a = readLine.trim.toInt
    val Array(reachHour, reachMinute) = readLine.trim.split(' ').map(_.toInt)
    val range = HourRange(Hour(17, 30), Hour(19, 30))
    if (range.contains(Hour(startHour, startMinute)) || range.contains(Hour(reachHour, reachMinute))) {
      if (math.abs(icDist(a - 1) - icDist(d - 1)) <= 40) {
        println((icFee(a - 1)(d - 1) + 1) / 2 * 50)
      }else{
        println(icFee(a - 1)(d - 1) * 50)
      }
    }else {
      println(icFee(a - 1)(d - 1) * 50)
    }
    solve
  }
  case class HourRange(from: Hour, to: Hour) {
    def contains(that: Hour): Boolean = from <= that && that <= to
  }
  case class Hour(hour: Int, minute: Int) extends Ordered[Hour] {
    override def compare(that: Hour): Int = (hour * 60 + minute).compare(that.hour * 60 + that.minute)
  }

  implicit class Extention[T](value :T){
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}
