#include<iostream>
#include<cstdlib>
using namespace std;

int charge(int in, int out);
int dist(int in, int out);
int checkhalf(int ih, int im, int oh, int om, int dis);
int half(int y);

int main(){
	while(1){
		int inic, inh, inm, outic, outh, outm, yen, kyori;
		cin >>inic;
		if(inic == 0) break;
		cin >>inh >>inm >>outic >>outh >>outm;
		
		yen = charge(inic, outic);
		kyori = dist(inic, outic);
		if(checkhalf(inh, inm, outh, outm, kyori)) yen = half(yen);
		
		cout <<yen <<endl;
	}
	return 0;
}

int charge(int in, int out){
	if(in == 1){
		if(out == 2) return 300;
		if(out == 3) return 500;
		if(out == 4) return 600;
		if(out == 5) return 700;
		if(out == 6) return 1350;
		if(out == 7) return 1650;
	} else if(in == 2){
		if(out == 1) return 300;
		if(out == 3) return 350;
		if(out == 4) return 450;
		if(out == 5) return 600;
		if(out == 6) return 1150;
		if(out == 7) return 1500;
	} else if(in == 3){
		if(out == 1) return 500;
		if(out == 2) return 350;
		if(out == 4) return 250;
		if(out == 5) return 400;
		if(out == 6) return 1000;
		if(out == 7) return 1350;
	} else if(in == 4){
		if(out == 1) return 600;
		if(out == 2) return 450;
		if(out == 3) return 250;
		if(out == 5) return 250;
		if(out == 6) return 850;
		if(out == 7) return 1300;
	} else if(in == 5){
		if(out == 1) return 700;
		if(out == 2) return 600;
		if(out == 3) return 400;
		if(out == 4) return 250;
		if(out == 6) return 600;
		if(out == 7) return 1150;
	} else if(in == 6){
		if(out == 1) return 1350;
		if(out == 2) return 1150;
		if(out == 3) return 1000;
		if(out == 4) return 850;
		if(out == 5) return 600;
		if(out == 7) return 500;
	} else if(in == 7){
		if(out == 1) return 1650;
		if(out == 2) return 1500;
		if(out == 3) return 1350;
		if(out == 4) return 1300;
		if(out == 5) return 1150;
		if(out == 6) return 500;
	}
}

int dist(int in, int out){
	if(in == 1){
		if(out == 2) return 6;
		if(out == 3) return 13;
		if(out == 4) return 18;
		if(out == 5) return 23;
		if(out == 6) return 43;
		if(out == 7) return 58;
	} else if(in == 2){
		if(out == 1) return 6;
		if(out == 3) return 7;
		if(out == 4) return 12;
		if(out == 5) return 17;
		if(out == 6) return 37;
		if(out == 7) return 52;
	} else if(in == 3){
		if(out == 1) return 13;
		if(out == 2) return 7;
		if(out == 4) return 5;
		if(out == 5) return 10;
		if(out == 6) return 30;
		if(out == 7) return 45;
	} else if(in == 4){
		if(out == 1) return 18;
		if(out == 2) return 12;
		if(out == 3) return 5;
		if(out == 5) return 5;
		if(out == 6) return 25;
		if(out == 7) return 40;
	} else if(in == 5){
		if(out == 1) return 23;
		if(out == 2) return 17;
		if(out == 3) return 10;
		if(out == 4) return 5;
		if(out == 6) return 20;
		if(out == 7) return 35;
	} else if(in == 6){
		if(out == 1) return 43;
		if(out == 2) return 37;
		if(out == 3) return 30;
		if(out == 4) return 25;
		if(out == 5) return 20;
		if(out == 7) return 15;
	} else if(in == 7){
		if(out == 1) return 58;
		if(out == 2) return 52;
		if(out == 3) return 45;
		if(out == 4) return 40;
		if(out == 5) return 35;
		if(out == 6) return 15;
	}
}

int checkhalf(int ih, int im, int oh, int om, int dis){
	int flag = 0;
	if(dis > 40){	//s£ª40kmæè·¢
		return 0;
	}
	
	if(ih > oh || (ih == oh && im > om)){	//úðÜ½¢¾
		if((ih > 17 || (ih == 17 && im >= 30)) && (ih < 19 || (ih == 19 && im <= 30))){	//üÁ½Ôª¼zÍÍàÅ
			if(oh < 17 || (oh == 17 && om < 30)){	//o½ÔªúðÜ½¢Å¼zÍÍO¾Á½
				return 1;
			}
		} else{
			return 0;
		}
		if(ih > 19 || (ih == 19 && im > 30)){	//üÁ½Ôª¼zÍÍæèàãÅ
			if((oh > 17 || (oh == 17 && om >= 30)) && (oh < 19 || (oh == 19 && om <= 30))){	//o½ÔªúðÜ½¢Å¼zÍÍà¾Á½
				return 1;
			}
		} else{
			return 0;
		}
	}
	
	if((ih < 17 || (ih == 17 && im <= 30)) && (oh > 17 || (oh == 17 && om >= 30)) && (oh < 19 || (oh == 19 && om < 30))){	//17æèO©1730ªÈOÉüèA17æèã©1730ªÈ~A©ÂA19æèO©1930ªæèOÉo½
		flag = 1;
	} else if((ih > 17 || (ih == 17 && im > 30)) && (ih < 19 || (ih == 19 && im <= 30)) && (oh > 19 || (oh == 19 && om >= 30))){	//àµ­ÍA17æèã©1730ªæèãA©ÂA19æèO©1930ªÈOÉüèA19æèã©1930ªÈ~Éo½
		flag = 1;
	}
	if(ih == oh && im == om && flag){	//flagª1ÅAüéÔÆoéÔª¯¶¾Á½
		flag = 0;
	}
	return flag;
}

int half(int y){
	int ans;
	double temp = y;
	temp /= 2.0;
	temp = ceil(temp);
	ans = (int)temp;
	if(ans % 50 != 0){
		ans = ans + abs((ans % 50) - 50);
	}
	return ans;
}