distance = [
    [ 0,  6, 13, 18, 23, 43, 58],
    [ 6,  0,  7, 12, 17, 37, 52],
    [13,  7,  0,  5, 10, 30, 45],
    [18, 12,  5,  0,  5, 25, 40],
    [23, 17, 10,  5,  0, 20, 35],
    [43, 37, 30, 25, 20,  0, 15],
    [58, 52, 45, 45, 40, 35,  0]
]
price = [
    [   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]
]

def judge(x, y, d, a):
    if distance[d][a] <= 40 and \
        1050 <= x <= 1170 or 1050 <= y <= 1170:
        return True
    else:
        return False

while True:
    d = int(input()) - 1
    if d < 0: break
    hd, md = map(int, input().split())
    a = int(input()) - 1
    ha, ma = map(int, input().split())

    x, y = (hd*60+md), (ha*60+ma)
    fee = price[d][a]

    if judge(x, y, d, a):
        fee = ((fee // 2 - 1) // 50 + 1) * 50
    print(fee)

