#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main()
{
  int data[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 s;
  for(;cin>>s,s;)
    {
      int h,m,e;
      int start,end;
      cin>>h>>m;
      start=h*60+m;
      cin>>e;
      cin>>h>>m;
      end=h*60+m;
      if(s>e)
	swap(s,e);
      s--;e--;
      int ans;
      if((start>=17*60+30||end<=19*60+30)&&data[e][s]<=40)
       ans=data[s][e]/2;
      else
	ans=data[s][e];
      for(;ans%50!=0;)
	ans++;
      cout<<ans<<endl;
    }
}