#include<iostream>
using namespace std;
int T[7][7]={
	{-1,300,500,600,700,1350,1650},
	{6,-1,350,450,600,1150,1500},
	{13,7,-1,250,400,1000,1350},
	{18,12,5,-1,250,850,1300},
	{23,17,10,5,-1,600,1150},
	{43,37,30,25,20,-1,500},
	{58,52,45,40,35,15}
};
main()
{
	int A;
	while(cin>>A,A)
	{
		int h,m;cin>>h>>m;m+=h*60;
		int B,m2;cin>>B>>h>>m2;m2+=h*60;
		if(A>B)A^=B^=A^=B;
		A--,B--;
		bool flag=T[B][A]<=40;
		if(!(1050<=m&&m<=1170||1050<=m2&&m2<=1170))flag=false;
		int ans=T[A][B];
		if(flag)ans>>=1;
		while(ans%50)ans++;
		cout<<ans<<endl;
	}
}

