#include<iostream>
using namespace std;
int main(){
	
	int d[7][7] = 
	{{ -1,300,500,600,700,1350,1650}
	,{  6, -1,350,450,600,1150,1500}
	,{ 13,  7,  0,250,400,1000,1350}
	,{ 18, 12,  5,  0,250, 850,1300}
	,{ 23, 17, 10,  5, -1, 600,1150}
	,{ 43, 37, 30, 25, 20,  -1, 500}
	,{ 58, 52, 45, 40, 35,  15,  -1}};
	
	

	int s,t,hs,ms,ht,mt;
	while(cin>>s,s){
		cin>>hs>>ms>>t>>ht>>mt;
		ms += hs*60;
		mt += ht*60;
		s--,t--;
		bool flg = false;
		if((17*60+30<=ms&&ms<=19*60+30)
		 ||(17*60+30<=mt&&mt<=19*60+30))flg = true;
		int price = d[s][t];
		if(d[t][s] <= 40 && flg){
			price/=2;
			if(price%50!=0){
				price += 50 - (price%50);
			}
		}
		cout<<price<<endl;
	}

	return 0;
}