// 2011/03/01 Tazoe

#include <iostream>
using namespace std;

int main()
{
	int tol[8][8] = {{  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1},
	                 {  -1,   0, 300, 500, 600, 700,1350,1650},
	                 {  -1, 300,   0, 350, 450, 600,1150,1500},
	                 {  -1, 500, 350,   0, 250, 400,1000,1350},
	                 {  -1, 600, 450, 250,   0, 250, 850,1300},
	                 {  -1, 700, 600, 400, 250,   0, 600,1150},
	                 {  -1,1350,1150,1000, 850, 600,   0, 500},
	                 {  -1,1650,1500,1350,1300,1150, 500,   0}};

	int dis[8][8] = {{  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1},
	                 {  -1,   0,   6,  13,  18,  23,  43,  58},
	                 {  -1,   6,   0,   7,  12,  17,  37,  52},
	                 {  -1,  13,   7,   0,   5,  10,  30,  45},
	                 {  -1,  18,  12,   5,   0,   5,  25,  40},
	                 {  -1,  23,  17,  10,   5,   0,  20,  35},
	                 {  -1,  43,  37,  30,  25,  20,   0,  15},
	                 {  -1,  58,  52,  45,  40,  35,  15,   0}};

	while(true){
		int fic;
		cin >> fic;
		if(fic==0)
			break;

		int fh, fm;
		cin >> fh >> fm;
		int tic;
		cin >> tic;
		int th, tm;
		cin >> th >> tm;

		int fhm = fh*100+fm;
		int thm = th*100+tm;

		if(((fhm>=1730&&fhm<=1930)||(thm>=1730&&thm<=1930))&&dis[fic][tic]<=40)
			cout << (tol[fic][tic]/50+1)/2*50 << endl;
		else
			cout << tol[fic][tic] << endl;
	}

	return 0;
}