#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e15+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int table[7][7] = {
	{ INF, 300, 500, 600, 700, 1350, 1650 },
	{ 6, INF, 350, 450, 600, 1150, 1500 },
	{ 13, 7, INF, 250, 400, 1000, 1350 },
	{ 18, 12, 5, INF, 250, 850, 1300 },
	{ 23, 17, 10, 5, INF, 600, 1150 },
	{ 43, 37, 30, 25, 20, INF, 500 },
	{ 58, 52, 45, 40, 35, 15, INF }
};

int dic, ric, h[10], m[10];

int main(){
	while (cin >> dic&&dic){
		cin >> h[0] >> m[0] >> ric >> h[1] >> m[1];
		dic--; ric--;
		int s = h[0] * 60 + m[0], e = h[1] * 60 + m[1];
		if ((s <= 17 * 60 + 30 && e >= 17 * 60 + 30 && e <= 19 * 60 + 30)
			|| (s >= 17 * 60 + 30 && s <= 19 * 60 + 30 && e >= 19 * 60 + 30)){
			if (table[ric][dic] <= 40){
				int num = table[dic][ric] / 2;
				if (num % 50 != 0){
					num += 25;
				}
				cout << num << endl;
			}
			else{
				cout << table[dic][ric] << endl;
			}
		}
		else{
			cout << table[dic][ric] << endl;
		}
	}
}