#include<iostream>
using namespace std;

int cost[8][8] = 
{
{},
{0,0,300,500,600,700,1350,1650},
{0,6,0,350,450,600,1150,1500},
{0,13,7,0,250,450,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 l,lh,lm,a,ah,am;

int max(){
	if(l<a)
		return l;
	else
		return a;
}

int min(){
	if(l>a)
		return l;
	else
		return a;
}

int main(){
	while(true){
		cin >> l;
		if(!l)
			return 0;
		cin >> lh >> lm;
		lm+=lh*100;
		cin >> a;
		cin >> ah >> am;
		am+=ah*100;
		if(cost[min()][max()] > 40)
			cout << cost[max()][min()] << endl;
		else if(!((lm>=1730&&lm<=1930)||(am>=1730&&am<=1930)))
			cout << cost[max()][min()] << endl;
		else{
			bool flag = false;
			if((cost[max()][min()]/2)%50)
				flag = true;
			int ans = cost[max()][min()]/2-(cost[max()][min()]/2)%50;
			if(flag)
				ans+=50;
			cout << ans << endl;
		}
	}
}