#include<iostream>
#include<algorithm>
#define S 1050 //1050min = 17:30 //
#define G 1170 //1170min = 19:30 //
using namespace std;
int kiriage(int);
int main(){
	int tmp[7][7]={{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,52,45,40,35,15,0}};

	int sIC,sh,sm,gIC,gh,gm,smin,gmin,cost;
	while(cin >> sIC && sIC){
		cin >> sh >> sm >> gIC >> gh >> gm;
		smin = sh * 60 + sm;
		gmin = gh * 60 + gm;
		if(sIC>gIC)swap(sIC,gIC);
		cout << ((((smin >= S && smin <= G) || (gmin >= S && gmin <= G)) && tmp[gIC-1][sIC-1]<=40) ?
			 		kiriage(tmp[sIC-1][gIC-1]/2) : tmp[sIC-1][gIC-1]) << endl;
	}
}
int kiriage(int cost){
	return (cost%50==0?cost:cost/50*50+50);
}