#include<bits/stdc++.h>
using namespace std;
 
int main(){
    int a[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 b;
    while(cin>>b,b){
	int c,d,e,f,g;
	scanf("%d%d%d%d%d",&c,&d,&e,&f,&g);
	c=c*60+d;
	f=f*60+g;
	if(c>=1050&&c<=1170||f>=1050&&f<=1170){
	    if(a[max(b,e)-1][min(b,e)-1]<=40){int o=a[min(b,e)-1][max(b,e)-1];if(o%50)o+=50-o%50;printf("%d\n",o);}
	    else printf("%d\n",a[min(b,e)-1][max(b,e)-1]);
	    }
	    else printf("%d\n",a[min(b,e)-1][max(b,e)-1]);
	}
    }