#include <bits/stdc++.h>
int p[7][7] = {{0, 6, 10, 12, 14, 27, 33}, {6, 0, 7, 9, 12, 23, 30},
               {13, 7, 0, 5, 8, 20, 27},   {18, 12, 5, 0, 5, 17, 26},
               {23, 17, 10, 5, 0, 12, 23}, {43, 37, 30, 25, 20, 0, 10},
               {58, 52, 45, 40, 35, 15, 0}};
int main() {
  int n1, h1, m1, n2, h2, m2, i, j, k, s, e;
  while (scanf("%d", &n1) && n1) {
    scanf("%d%d%d%d%d", &h1, &m1, &n2, &h2, &m2);
    s = h1 * 60 + m1;
    e = h2 * 60 + m2;
    if (n1 > n2) i = n1, n1 = n2, n2 = i;
    n1--;
    n2--;
    if (((19 * 60 + 30) >= s && s >= (17 * 60 + 30)) ||
        ((19 * 60 + 30) >= e && e >= (17 * 60 + 30)) && p[n2][n1] <= 40)
      printf("%d\n", (p[n1][n2] + 1) / 2 * 50);
    else
      printf("%d\n", p[n1][n2] * 50);
  }
  return 0;
}
