sheet = [[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],
         [59, 52, 45, 40, 35,  15,   0]]

while 1:
    s = int(raw_input())-1
    if s == -1: break
    st = int(raw_input().replace(" ",""))
    g = int(raw_input())-1
    gt = int(raw_input().replace(" ",""))
    s,g = min(s,g),max(s,g)
    dist = sheet[g][s]
    cost = sheet[s][g]
    if dist <= 40 and (1730 <= st <= 1930 or 1730 <= gt <= 1930):
        cost = cost/2
        cost = cost/100*100 + (cost%100+49)/50*50
    print cost