//07
#include<iostream>
#include<algorithm>

#define T(h,m) ((h)*60+(m))

using namespace std;

int main(){
  for(int s;cin>>s,s;){
    int sh,sm;
    int t,th,tm;
    cin>>sh>>sm>>t>>th>>tm;
    s--;
    t--;
    int f[7][7]={{0,300,500,600,700,1350,1650},{0,0,350,450,600,1150,1500},{0,0,0,250,400,1000,1350},{0,0,0,0,250,850,1300},{0,0,0,0,0,600,1150},{0,0,0,0,0,0,500},{0,0,0,0,0,0,0}};
    int d[7][7]={{0,0,0,0,0,0,0},{6,0,0,0,0,0,0},{13,7,0,0,0,0,0},{18,12,5,0,0,0,0},{23,17,10,5,0,0,0},{43,37,30,25,20,0,0},{58,52,45,40,35,15}};
    int sum=max(f[s][t],f[t][s]);
    if(!((T(sh,sm)<T(17,30)&&T(19,30)<T(th,tm))||T(sh,sm)>T(19,30)||T(th,tm)<T(17,30))&&max(d[s][t],d[t][s])<=40){
      sum=sum/2+((sum%100==50)?25:0);
    }
    cout<<sum<<endl;
  }
  return 0;
}