#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>
#include<iterator>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define rep(i,n) for(int i=0; i<n; i++)
#define INF (1<<20)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

int table[][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 to, from;
	int d, hd, md,sd;
	int a, ha, ma,sa;
	while (cin >> d, d) {
		cin >> hd >> md;
		cin >> a >> ha >> ma;
		d--, a--;

		to = 19 * 60 * 60 + 30 * 60;
		from = 17 * 60 * 60 + 30 * 60;
		sd = hd * 60 * 60 + md * 60;
		sa = ha * 60 * 60 + ma * 60;

		if (from <= sd && to >= sa || from >= sd && to >= sa || from <= sd && to <= sa) {
			if (table[a][d] <= 40) {
				int ans = table[d][a] / 2;
				if (ans % 50 != 0)ans += 25;
				cout << ans << endl;
			}
			else cout << table[d][a] << endl;
		}
		else
			cout << table[d][a] << endl;
	}
	return 0;
}