#include <iostream>

using namespace std;

int a[7][7] = {
	{  0,   300,  500,  600,  700, 1350, 1650},
	{ 300,    0,  350,  450,  600, 1150, 1500},
	{ 500,  350,    0,  250,  400, 1000, 1350},
	{ 600,  450,  250,    0,  250,  850, 1300},
	{ 700,  600,  400,  250,    0,  600, 1150},
	{1350, 1150, 1000,  850,  600,    0, 500},
	{1650, 1500, 1350, 1300, 1150,  500,   0}
};

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

int main(int argc, char **argv)
{
	int m1, s1, m2, s2, ic1, ic2;
	while(1){
		cin >> ic1;
		if(ic1 == 0){
			break;
		}
		cin >> m1 >> s1;
		cin >> ic2;
		cin >> m2 >> s2;
		ic1--;
		ic2--;
		if(!((m1 == 17 && s1 >= 30)||(m1 == 18)||(m1 == 19 && s1 <= 30))
		&&!((m2 == 17 && s2 >= 30)||(m2 == 18)||(m2 == 19 && s2 <= 30))){
			cout << a[ic1][ic2] << endl;
			continue;
		}
		
		if(d[ic1][ic2] <= 40){
			m1 = a[ic1][ic2]/2;
			m2 = m1 % 50;
			if(m2 != 0){
				m1 = m1 - m2 + 50;
			}
			cout << m1 << endl;
		} else {
			cout << a[ic2][ic1] << endl;
		}
		
	}
	return 0;
}