#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>

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 1000000007
#define EPS 1e-10

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

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

int s, g, h1, m1, h2, m2;
int main(){
	while (cin >> s&&s){
		cin >> h1 >> m1 >> g >> h2 >> m2;
		g--;
		s--;
		int sum; 
		if (table[g][s] <= 40){
			if (h1 < 17 || (h1 == 17 && m1 <= 30)){
				if (h2 < 19 || (h2 == 19 && m2 < 30)){
					if (table[s][g] / 2 % 50 == 0){
						sum = table[s][g] / 2 / 50 * 50;
					}
					else {
						sum = table[s][g] / 2 / 50 * 50 + 50;
					}
				}
				else {
					sum = table[s][g];
				}
			}
			else if (h1 > 17 || (h1 == 17 && m1 > 30)){
				if (h2 > 19 || (h2 == 19 && m2 >= 30)){
					if (table[s][g] / 2 % 50 == 0){
						sum = table[s][g] / 2 / 50 * 50;
					}
					else {
						sum = table[s][g] / 2 / 50 * 50 + 50;
					}
				}
				else{
					sum = table[s][g];
				}
			}
		}
		else sum = table[s][g];
		cout << sum << endl;
	}
}