#include <bits/stdc++.h>

using namespace std;

const int Distance[8][8] = { 
	{ 0, 00, 00, 00, 00, 00, 00, 00 },
	{ 0, 00, 06, 13, 18, 23, 43, 58 }, 
	{ 0, 06, 00, 07, 12, 17, 37, 52 }, 
	{ 0, 13, 07, 00, 05, 10, 30, 45 }, 
	{ 0, 18, 23, 43, 00, 05, 25, 40 }, 
	{ 0, 23, 17, 10, 05, 00, 20, 35 }, 
	{ 0, 43, 37, 30, 25, 20, 00, 15 },
	{ 0, 58, 52, 45, 40, 35, 15, 00 }, 
};

const int Money[8][8] = {
	{ 0,  000, 000,  000,  000,  000,  0000, 0000 },
	{ 0,  000, 300,  500,  600,  700,  1350, 1650 },
	{ 0,  300, 000,  350,  450,  600,  1150, 1500 },
	{ 0,  500, 350,  000,  250,  400,  1000, 1350 },
	{ 0,  600, 450,  250,  000,  250,  850,  1300 },
	{ 0,  700, 600,  400,  250,  000,  600,  1150 },
	{ 0, 1350, 1150, 1000, 850,  600,  000,  500  },
	{ 0, 1650, 1500, 1350, 1300, 1150, 500,  000  },
};

const int TimeMin = 17 * 60 + 30;
const int TimeMax = 19 * 60 + 30;

int main() {
	int d, a, hd, md, ha, ma, timeD, timeA, sum;

	while (cin >> d, d) {
		cin >> hd >> md;
		cin >> a >> ha >> ma;
		sum = Money[d][a];
		timeD = hd * 60 + md;
		timeA = ha * 60 + ma;
		if ( Distance[d][a] <= 40 && (timeD>=TimeMin && timeD<=TimeMax || timeA>=TimeMin && timeA<=TimeMax) ) {
			sum /= 2;
			if (sum % 50 != 0) {
				sum += 50 - sum % 50;
			}
			
		}
		cout << sum << endl;
	}

	return 0;
}

