#include<cstdio>
using namespace std;
int main(void)
{
	int hyou[8][8]={{0,0,0,0,0,0,0,0},
					{0,0,300,500,600,700,1350,1650},
					{0,6,0,350,450,600,1150,1500},
					{0,13,7,0,250,400,1000,1350},
					{0,18,12,5,0,250,850,1300},
					{0,23,17,10,5,0,600,1150},
					{0,43,37,30,25,20,0,500},
					{0,58,52,45,40,35,15,0}};
	int ic1,ic2,h1,h2,m1,m2,i,j,wk,t1,t2,kin;
	
	while(1)	{
		scanf("%d",&ic1);
		if(ic1==0)	break;
		scanf("%d %d",&h1,&m1);
		scanf("%d",&ic2);
		scanf("%d %d",&h2,&m2);
		if(ic1>ic2)	{
			wk=ic1;
			ic1=ic2;
			ic2=wk;
		}
		t1=h1*60+m1;
		t2=h2*60+m2;
		if(((t1>=17*60+30 && t1<=19*60+30) || (t2>=17*60+30 && t2<=19*60+30)) && hyou[ic2][ic1]<=40)	{
			if(hyou[ic1][ic2]%100!=0)	{
				kin=(hyou[ic1][ic2]+50)/2;
			}
			else {
				kin=hyou[ic1][ic2]/2;
			}
		}
		else {
			kin=hyou[ic1][ic2];
		}
		printf("%d\n",kin);
	}
	return 0;
}
