#include <bits/stdc++.h>
using namespace std;
int main() {
  char c1[2], c2[2];
  int h, m, ic[2], f[2], i, k;
  int g[6][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}};
  while (true) {
    for (i = 0; i < 2; i++) {
      cin >> ic[i];
      if (ic[i] == 0) return 0;
      ic[i]--;
      cin >> c1 >> c2;
      h = (c1[0] - '0') * 10 + (c1[1] - '0');
      m = (c2[0] - '0') * 10 + (c2[1] - '0');
      if ((h == 18) || (h == 17 && m > 30) || (h == 19 && m < 30))
        f[i] = 2;
      else if ((h == 17 && m == 30) || (h == 19 && m == 30))
        f[i] = 1;
      else
        f[i] = 0;
    }
    k = g[ic[0]][ic[1]];
    if (!((ic[1] == 6 && (ic[0] >= 0 && ic[0] < 3)) ||
          (ic[0] == 0 && ic[1] == 5) || f[0] == f[1]))
      k = (k % 100) > 0 ? k / 2 + 25 : k / 2;
    cout << k << endl;
  }
  return 0;
}
