A = [
    [0, 300, 500, 600, 700, 1350, 1650],
    [6, 0, 350, 450, 600, 1150, 1500],
    [13, 7, 0, 250, 400, 1000, 1350],
    [18, 12, 5, 0, 250, 850, 1300],
    [23, 17, 10, 5, 0, 600, 1150],
    [43, 37, 30, 25, 20, 0, 500],
    [58, 52, 45, 40, 35, 15, 0],
]

while 1:
    d = int(input()); d -= 1
    if d == -1:
        break
    hd, md = map(int, input().split())
    dt = hd*100 + md
    a = int(input()); a -= 1
    ha, ma = map(int, input().split())
    at = ha*100 + ma
    if not a <= d:
        d, a = a, d
    if A[d][a] <= 40 and (1730 <= dt <= 1930 or 1730 <= at <= 1930):
        print((A[a][d] // 2 + 49)//50*50)
    else:
        print(A[a][d])

