from collections import namedtuple

Data = namedtuple("Data", "rate distance")

SENTINEL = None
NOTHING = None
RATE_TABLE = [
    [SENTINEL],
    [SENTINEL, NOTHING, Data(300, 6), Data(500, 13), Data(600, 18), Data(700, 23), Data(1350, 43), Data(1650, 58)],
    [SENTINEL, Data(300, 6), NOTHING, Data(350, 7), Data(450, 12), Data(600, 17), Data(1150, 37), Data(1500, 52)],
    [SENTINEL, Data(500, 13), Data(350, 7), NOTHING, Data(250, 5), Data(400, 10), Data(1000, 30), Data(1350, 45)],
    [SENTINEL, Data(600, 18), Data(450, 12), Data(250, 5), NOTHING, Data(250, 5), Data(850, 25), Data(1300, 40)],
    [SENTINEL, Data(700, 23), Data(600, 17), Data(400, 10), Data(250, 5), NOTHING, Data(600, 20), Data(1150, 35)],
    [SENTINEL, Data(1350, 43), Data(1150, 37), Data(1000, 30), Data(850, 25), Data(600, 20), NOTHING, Data(500, 15)],
    [SENTINEL, Data(1650, 58), Data(1500, 52), Data(1350, 45), Data(1300, 40), Data(1150, 35), Data(500, 15), NOTHING]
]
DISTANCE_LIMIT = 40
TIME_LIMIT1 = 17 * 60 + 30
TIME_LIMIT2 = 19 * 60 + 30

while True:

    leave_number = int(input())

    if leave_number == 0:
        break

    leave_time = [int(item) for item in input().split(" ")]

    arrive_number = int(input())
    arrive_time = [int(item) for item in input().split(" ")]

    data = RATE_TABLE[leave_number][arrive_number]

    rate = data.rate

    if data.distance <= DISTANCE_LIMIT:

        time1 = leave_time[0] * 60 + leave_time[1]
        time2 = arrive_time[0] * 60 + arrive_time[1]

        if TIME_LIMIT1 <= time1 <= TIME_LIMIT2 or TIME_LIMIT1 <= time2 <= TIME_LIMIT2:
            rate //= 2

            if rate % 50 != 0:
                rate = rate + 50 - rate % 50

    print(rate)

