#include<iostream>
using namespace std;
int main(){
  int mat1[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 mat2[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 j,k,d,a,md[10000],hd[10000],ma[10000],ha[10000],fare[10000];
  for(j=0;j>=0;j++){
    cin >> d;
    if(d == 0)
      break;
    cin >> hd[j] >> md[j];
    cin >> a;
    if(a == 0)
      break;
    cin >> ha[j] >> ma[j];
    fare[j] = mat2[d-1][a-1];
    if(100*hd[j]+md[j]<=1930 && 100*hd[j]+md[j]>=1730 || 100*ha[j]+ma[j]<=1930 && 100*ha[j]+ma[j]>=1730){
      if(mat1[d-1][a-1] <=40){
      fare[j] = fare[j]/2;
      if(fare[j]%10 == 5)
	fare[j] += 25;
      }}
  }
  for(k=0;k<=j;k++)
    cout << fare[k] << endl;
  return 0;
}