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,h1,m1,fin,h2,m2=fin,h2,m2,init,h1,m1
    if (h1*100+m1)-1730>=0 or 1930-(h2*100+m2)>=0 and (init,fin) not in fbd:
        temp=table[init-1][fin-1]/2
        if temp%100!=0:
            temp+=abs(50-temp%100)
        print temp
    else:
        print table[init-1][fin-1]