#include <iostream>
#include <string>
using namespace std;

int s2i(string a,string b){
  int h=0,m=0;
  for(int i=0;i<a.size();i++) h*=10,h+=a[i]-'0';
  for(int i=0;i<b.size();i++) m*=10,m+=b[i]-'0';
  return h*60+m;
}

int main() {
  int fee[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 s,g;
  string a[2],b[2];
  while(1 ){
    cin>> s;
    if(s==0)break;
    cin >> a[0] >> a[1];
    cin >> g;
    cin >> b[0]>>b[1];
    s--,g--;
    int ans = fee[s][g];
    int st = s2i(a[0],a[1]);
    int gt = s2i(b[0],b[1]);
    if(D[s][g]<=40 && ((60*17+30<= st&&st<=60*19+30)||(60*17+30<=gt&&gt<=60*19+30)))ans/=2;
    if(ans%50!=0) ans = (ans/50+1)*50;
    cout << ans <<endl;
  }




  return 0;
}