#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

vector<vector<int>>money{
	{0,300,500,600,700,1350,1650},
	{0,0,350,450,600,1150,1500},
	{0,0,0,250,400,1000,1350},
	{0,0,0,0,250,850,1300},
	{0,0,0,0,0,600,1150},
	{0,0,0,0,0,0,500},
	{ 0,0,0,0,0,0,0 },

};
vector<int>dis{ 6,7,5,5,20,15 };

int main() {
	while (1) {
		int d; cin >> d;
		if (!d)break;
		int start,goal;
		{

			int h, m; cin >> h >> m;
			start = h * 60 + m;
		}
		int a; cin >> a;
		{

			int h, m; cin >> h >> m;
			goal = h * 60 + m;
		}
		d--; a--;
		if (d < a)swap(d, a);
		int cost = money[a][d];
		cost /= 50; 
		int distance = 0;
		for (int i = a; i < d; ++i) {
			distance += dis[i];
		}
		bool flag = false;
		if ((start <= 1170 || 1050 <= goal)&&!(start<1050&&1170<goal))flag = true;
		if (distance <= 40 && flag)cost = (cost + 1) / 2;
		cost *= 50;
		cout << cost << endl;
	}
	return 0;
}