#include <iostream>

using namespace std;

#define INF 1 << 28

int fee[7][7] = {
	{INF, 300, 500, 600, 700, 1350, 1650},
	{300, INF, 350, 450, 600, 1150, 1500},
	{500, 350, INF, 250, 400, 1000, 1350},
	{600, 450, 250, INF, 250, 850, 1300},
	{700, 600, 400, 250, INF, 600, 1150},
	{1350, 1150, 1000, 850, 600, INF, 500},
	{1650, 1500, 1350, 1300, 1150, 500, INF}
};

int dist[7][7] = {
	{INF, 6, 13, 18, 23, 43, 58},
	{6, INF, 7, 12, 17, 37, 52},
	{13, 7, INF, 5, 10, 30, 45},
	{18, 12, 5, INF, 5, 25, 40},
	{23, 17, 10, 5, INF, 20, 35},
	{43, 37, 30, 25, 20, INF, 15},
	{58, 52, 45, 40, 35, 15, INF}
};

bool in_bargain_time(int h, int m) {
	return (17 * 60+ 30 <= h * 60 + m && h * 60 + m <= 19 * 60 + 30);
}

int main() {
	int from, to, hf, mf, ht, mt;
	while (cin >> from, from) {
		cin >> hf >> mf >> to >> ht >> mt;
		if ( (in_bargain_time(hf, mf) || in_bargain_time(ht, mt)) && dist[from-1][to-1] <= 40) {
			cout << (fee[from-1][to-1] / 2 + 49) / 50 * 50 << endl;
		} else {
			cout << fee[from-1][to-1] << endl;
		}
	}
	return 0;
}
		