#include <bits/stdc++.h>
using namespace std;

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


int d,hd,md,a,ha,ma;

int to_m(int h,int m){
	return h * 60 + m;
}
bool time_ok(){
	int dt = to_m(hd,md);
	int at = to_m(ha,ma);
	int t1730 = to_m(17,30);
	int t1930 = to_m(19,30);
	return (dt >= t1730 && dt <= t1930) || (at >= t1730 && at <= t1930);
}
bool is_half(){
	return tb[a][d] <= 40 && time_ok();
}
int roundup(int f){
	int h = f % 50;
	if(h > 0){
		return f - h + 50;
	}
	else{
		return f;
	}
}
int fee(){
	return is_half() ? roundup(tb[d][a] / 2) : roundup(tb[d][a]);
}

int main(){
	while(cin >> d, d != 0){
		cin >> hd >> md >> a >> ha >> ma;
		cout << fee() << endl;
	}
	return 0;
}

