#include<iostream>
using namespace std;
int dist[7][7] = { {0,0,0,0,0,0,0},{6,0,0,0,0,0,0},{13,7,0,0,0,0,0},{18,12,5,0,0,0,0},{23,17,10,5,0,0,0},{43,37,30,25,20,0,0},{58,52,45,40,35,15,0} };
int money[7][7] = { {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} };
int a, b, c, d, e, f;
int main() {
	while (true) {
		cin >> a; if (a == 0) { break; }cin >> b >> c >> d >> e >> f; b = b * 60 + c; e = e * 60 + f; a--; d--;
		for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { if (dist[i][j] == 0) { dist[i][j] = dist[j][i]; }if (money[i][j] == 0) { money[i][j] = money[j][i]; } } }
		if (((b >= 1050 && b <= 1170) || (e >= 1050 && e <= 1170)) && dist[a][d] <= 40) { if (money[a][d] % 100 == 0) { cout << money[a][d] / 2 << endl; } else { cout << money[a][d] / 2 + 25 << endl; } }
		else { cout << money[a][d] << endl; }
	}
}