#include <iostream>
using namespace std;
int m[7][7][2] = {
  {{0,0},{6,300},{13,500},{18,600},{23,700},{43,1350},{58,1650}},
  {{6,300},{0,0},{7,350},{12,450},{17,600},{37,1150},{52,1500}},
  {{13,500},{7,350},{0,0},{5,250},{10,400},{30,1000},{45,1350}},
  {{18,600},{12,450},{5,250},{0,0},{5,250},{25,850},{40,1300}},
  {{23,700},{17,600},{10,400},{5,250},{0,0},{20,600},{35,1150}},
  {{43,1350},{37,1150},{30,1000},{25,850},{20,600},{0,0},{15,500}},
  {{58,1650},{52,1500},{45,1350},{40,1300},{35,1150},{15,500},{0,0}},
};
int main()
{
  int s,g,hs,ms,hg,mg,t,c;
  bool f;
  for(;;){
    cin >> s;
    if(s==0) break;
    cin >> hs >> ms;
    cin >> g;
    cin >> hg >> mg;
    f = false;
    t = hs * 60 + ms;
    if(1050<= t && t<=1170) f = true;
    t = hg * 60 + mg;
    if(1050<= t && t<=1170) f = true;
    if(m[s-1][g-1][0] > 40) f = false;
    c = m[s-1][g-1][1];
    if(f){
      c /= 2;
      if(c % 50 != 0) {
	c = c - (c % 50) + 50;
      }
    }
    cout << c << endl;
  }
  return 0;
}