#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int ceil(int n) {
	return (n / 50 + (n % 50 > 0)) * 50;
}
int cost[][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},
};
int dist[][7]{
	{},
	{6},
	{13,7},
	{18,12,5},
	{23,17,10,5},
	{43,37,30,25,20},
	{58,52,45,40,35,15}
};
int main() {
	int d;
	while (scanf("%d", &d), d) {
		int hd, md, a, ha, ma; scanf("%d%d%d%d%d", &hd, &md, &a, &ha, &ma);
		d--; a--;
		if (((1050 <= hd * 60 + md&&hd * 60 + md <= 1170) || (1050 <= ha * 60 + ma&&ha * 60 + ma <= 1170)) && dist[max(d, a)][min(d, a)] <= 40)
			printf("%d\n", ceil(cost[min(d, a)][max(a, d)] / 2));
		else printf("%d\n", cost[min(d, a)][max(a, d)]);
	}
}