#include <iostream>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
	P hyou[8][8];
	hyou[1][2] = hyou[2][1] = P(6, 300);
	hyou[1][3] = hyou[3][1] = P(13, 500);
	hyou[1][4] = hyou[4][1] = P(18, 600);
	hyou[1][5] = hyou[5][1] = P(23, 700);
	hyou[1][6] = hyou[6][1] = P(43, 1350);
	hyou[1][7] = hyou[7][1] = P(58, 1650);
	hyou[2][3] = hyou[3][2] = P(7, 350);
	hyou[2][4] = hyou[4][2] = P(12, 450);
	hyou[2][5] = hyou[5][2] = P(17, 600);
	hyou[2][6] = hyou[6][2] = P(37, 1150);
	hyou[2][7] = hyou[7][2] = P(52, 1500);
	hyou[3][4] = hyou[4][3] = P(5, 250);
	hyou[3][5] = hyou[5][3] = P(10, 400);
	hyou[3][6] = hyou[6][3] = P(30, 1000);
	hyou[3][7] = hyou[7][3] = P(45, 1350);
	hyou[4][5] = hyou[5][4] = P(5, 250);
	hyou[4][6] = hyou[6][4] = P(25, 850);
	hyou[4][7] = hyou[7][4] = P(40, 1300);
	hyou[5][6] = hyou[6][5] = P(20, 600);
	hyou[5][7] = hyou[7][5] = P(35, 1150);
	hyou[6][7] = hyou[7][6] = P(15, 500);
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int sh, sm;
		cin >> sh >> sm;
		int m;
		cin >> m;
		int gh, gm;
		cin >> gh >> gm;
		if (sh == 18 || gh == 18) {
			if (hyou[n][m].first <=  40) {
				int p = hyou[n][m].second;
				p /= 2;
				if (p % 100 == 25) {
					p += 25;
				} 
				cout << p << endl;
			} else {
				cout << hyou[n][m].second << endl;
			}
		} else if (sh == 17 || gh == 17) {
			int p = hyou[n][m].second;
			if (sh == 17) {
				if (sm >= 30) {
					if (hyou[n][m].first <= 40) {
						p /= 2;
						if (p % 100 == 25) {
							p += 25;
						}
					}
				} else if (gh == 17) {
					if (gm >= 30) {
						if (hyou[n][m].first <= 40) {
							p /= 2;
							if (p % 100 == 25) {
								p += 25;
							}
						}
					}
				} else if (gh == 19) {
					if (gm <= 30) {
						if (hyou[n][m].first <= 40) {
							p /= 2;
							if (p % 100 == 25) {
								p += 25;
							}
						}
					}
				}
			} else {
				if (gm >= 30) {
					if (hyou[n][m].first <= 40) {
						p /= 2;
						if (p % 100 == 25) {
							p += 25;
						}
					}
				}
			}
			cout << p << endl;
		} else if (sh == 19 || gh == 19) {
			int p = hyou[n][m].second;
			if (sh == 19) {
				if (sm <= 30) {
					if (hyou[n][m].first <= 40) {
						p /= 2;
						if (p % 100 == 25) {
							p += 25;
						}
					}
				}
			} else {
				if (gm <= 30) {
					if (hyou[n][m].first <= 40) {
						p /= 2;
						if (p % 100 == 25) {
							p += 25;
						}
					}
				}
			}
			cout << p << endl;
		} else {
			cout << hyou[n][m].second << endl;
		}
	}
	return 0;
}