data = {(1,2):(6,300), (1,3):(13,500), (1,4):(18,600), 
        (1,5):(23,700), (1,6):(43,1350), (1,7):(58,1650), 
        (2,3):(7,350), (2,4):(12,450), (2,5):(17,600), 
        (2,6):(37,1150),(2,7):(52,1500), (3,4):(5,250), 
        (3,5):(10,400), (3,6):(30,1000), (3,7):(45,1350), 
        (4,5):(5,250), (4,6):(25,850), (4,7):(40,1300), 
        (5,6):(20,600), (5,7):(35,1150), (6,7):(15,500)}

while True:
    s = input()
    if s == 0: break
    sh, sm = map(int, raw_input().split())
    g = input()
    gh, gm = map(int, raw_input().split())
    flag = False
    for h, m in [(sh, sm), (gh, gm)]:
        if h == 17 and 30 <= m:
            flag = True
            break
        elif h == 18:
            flag = True
            break
        elif h == 19 and m <= 30:
            flag = True
            break
    if s < g:
        key = (s,g)
    else:
        key = (g,s)
    if flag:
        if 40 < data[key][0]:
            flag = False
    if flag:
        p = str(data[key][1]/2)
        if int(p[-2:]) <= 50:
            print p[0]+'50'
        else:
            print str(int(p[0])+1)+'00'
    else:
        print data[key][1]