#include <bits/stdc++.h>
using namespace std;
double h(int h, int m) { return h + (double)m / 60; }
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;
    int gh, gm;
    cin >> s;
    if (s == 0) break;
    cin >> sh >> sm;
    cin >> g;
    cin >> gh >> gm;
    if (s > g) {
      int temp = s;
      s = g;
      g = temp;
    }
    bool half = false;
    if (h(sh, sm) >= h(17, 30) && h(sh, sm) <= h(19, 30) && base[g][s] <= 40) {
      half = true;
    } else if (h(gh, gm) >= h(17, 30) && h(gh, gm) <= h(19, 30) &&
               base[g][s] <= 40) {
      half = true;
    }
    if (half) {
      int tmp = (base[s][g] / 2 + 50) / 50 * 50;
      cout << tmp << endl;
    } else {
      cout << base[s][g] << endl;
    }
  }
  return 0;
}
