#include<stdio.h>

int main(){

int st1,st2;

int h1,h2,m1,m2;


int n[8][8]={
{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,052,45,40,35,15,0}
};

while(1){
scanf("%d",&st1);if(st1==0)break;

scanf("%d %d",&h1,&m1);

scanf("%d",&st2);
scanf("%d %d",&h2,&m2);

int t1=100*h1+m1;int t2=100*h2+m2;

int T1=st1,T2=st2,S=0;
if(st1>=st2){T1=st2;T2=st1;}

if((t1>=1730&&t2<=1930)&&n[T2-1][T1-1]<=40)
{S=(n[T1-1][T2-1])/2;}
else S=(n[T1-1][T2-1]);

printf("%d\n",S);
}
return 0;
}