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

	while(true){
		int ans = 0;
		int start, end;
		int start_h, end_h, start_m, end_m;
		int start_time, end_time;
		
		cin>>start;
		if( start == 0 )
			break;
		cin>>start_h>>start_m>>end>>end_h>>end_m;
		--start;--end;
		start_time = start_h * 100 + start_m;
		end_time = end_h * 100 + end_m;

		ans = val[start][end];
		if( dist[start][end] <= 40 &&
			(1730 <= start_time && start_time <= 1930) ||
			(1730 <= end_time && end_time <= 1930) ){
				ans /= 2;
		}
		if( ans % 50 != 0 ){
			ans = (ans / 50 + 1) * 50;
		}
		cout << ans << endl;
	}
	return 0;
}