#include<cmath>
#include<algorithm>
using namespace std;
int hyo[7][7]={
	{0,300,500,600,700,1350,1650},
	{6,0,350,450,600,1150,1500},
	{13,7,0,250,400,1000,1350},
	{18,12,5,0,250,850,1300},
	{23,17,10,5,0,600,1150},
	{43,37,30,25,20,0,500},
	{58,52,45,40,35,15,0}
};
int main(){
	int s,g, h,m,H,M, c;
	while(cin>>s,s){
		cin>>h>>m>>g>>H>>M;
		if(s>g)swap(s,g);
		c=hyo[s-1][g-1];
		if(hyo[g-1][s-1]<=40 &&( (1050<=h*60+m&&h*60+m<=1170)||(1050<=H*60+M&&H*60+M<=1170) ) )
		c=((int)ceil((c/2/50.0))*50;
		cout<<c<<endl;
	}
	return 0;
}