
#include<iostream>
using namespace std;


//料金2次元配列
const int wh_data[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}};

//時間入力構造体
typedef struct struct_time_input
{
	int number;
	int h;
	int m;
}S_time_input;

//半額時間
typedef struct struct_price
{
	int price_start;
	int price_end;
}S_price;

int main(void)
{
	//安い時間を宣言
	const S_price s_price = {60 * 17 + 30 , 60 * 19 + 30};

	while (true)
	{
		//入力変数
		S_time_input s_start = {0};
		S_time_input s_end = {0};
		//金
		int money = 0;
		//距離
		int length = 0;

		cin >> s_start.number;
		if (s_start.number == 0)	{	return 0;	};
		cin >> s_start.h >> s_start.m;

		cin >> s_end.number >> s_end.h >> s_end.m;

		money = wh_data[s_start.number - 1][s_end.number - 1];
		length = wh_data[s_end.number - 1][s_start.number - 1];

		//まず距離が40km以内
		if (length <= 40)
		{
			//それぞれの時間を計測
			int start_time = s_start.h * 60 + s_start.m;
			int end_time = s_end.h * 60 + s_end.m;

			//まずは範囲内
			if (start_time <= s_price.price_end && end_time >= s_price.price_start)
			{
				//両方範囲から外れているのならOUT
				if (start_time < s_price.price_start && end_time > s_price.price_end)
				{
					;
				}
				else
				{
					money /= 2;
					if (money % 50 != 0)
					{
						int sa = 0;
						sa = money % 50;
						money -= sa;
						money += 50;
					}
				}
			}
		}

		cout << money << endl;
	}

	return 0;
}