#include <iostream>
using namespace std;

int m(int h, int m) {
  return h*60+m;
}

int main() {
  int base[8][8] = {{0,0,0,0,0,0,0,0},
		    {0,0, 300,500,600,700,1350,1650},
		    {0,6, 0,  350,450,600,1150,1500},
		    {0,13,7,  0,  250,400,1000,1350},
		    {0,18,12, 5,  0,  250,850, 1300},
		    {0,23,17, 10, 5,  0,  600, 1150},
		    {0,43,37, 30, 25, 20, 0,   500},
		    {0,58,52, 45, 40, 35, 15,  0}};
  while(1) {
    int s,g;
    int sh, sm, gh, gm;
    cin >> s;
    if(s == 0) break;
    cin >> sh >> sm;
    cin >> g;
    cin >> gh >> gm;
    int temp;
    if(s > g) {
      temp = s;
      s = g;
      g = temp;
    }

    bool half = false;
    if(m(sh,sm) >= m(17,30) && m(sh,sm) <= m(19,30) && base[g][s] <= 40) {
      half = true;
    }
    else if(m(gh,gm) >= m(17,30) && m(gh,gm) <= m(19,30) && base[g][s] <= 40) {
      half = true;
    }
    if(half) {
      temp = (base[s][g] / 2 + 49) / 50 * 50;
      cout << temp << endl;
    } else {
      cout << base[s][g] << endl;
    }
  }
  return 0;
}