#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int info[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 main() {
	int s, st, e, et;
	while (cin >> s, s) {
		int h, m;
		cin >> h >> m;
		st = h * 60 + m;
		cin >> e;
		cin >> h >> m;
		et = h * 60 + m;
		
		s--; e--;
		int ans = info[s][e];
		if (st >= 17*60 + 30 && st <= 19*60 + 30 || et >= 17*60 + 30 && et <= 19*60 + 30) {
			if (info[e][s] <= 40) ans = ans % 100 == 0 ? ans / 2 : (ans + 100) / 2 - (ans / 2 % 50);
		}
		
		cout << ans << endl;
	}
	return 0;
}