table=[[0,300,500,600,700,1350,1650],
       [0,0,350,450,600,1150,1500],
       [0,0,0,250,400,1000,1350],
       [0,0,0,0,250,850,1300],
       [0,0,0,0,0,600,1150],
       [0,0,0,0,0,0,500]]
fbd=[(1,6),(1,7),(2,7),(3,7)]
while True:
    init=input()
    if init==0:break
    h1,m1=map(int,raw_input().split())
    fin=input()
    h2,m2=map(int,raw_input().split())
    if init>fin:
        init,fin=fin,init

    t1=h1*60+m1
    t2=h2*60+m2
    if 1050<=t1<=1170 or 1050<=t2<=1170 and (init,fin) not in fbd:
        temp=table[init-1][fin-1]/2
        if temp%100>0:
            temp+=50-temp%50
        print temp
    else:
        print table[init-1][fin-1]