#include <bits/stdc++.h>
int D[7] = {0, 6, 7, 5, 5, 20, 15}, S, G, Ts[2], Tg[2], d, i, r;
int p[7][7] = {{0, 30, 50, 60, 70, 135, 165}, {0, 0, 35, 45, 60, 115, 150},
               {0, 0, 0, 25, 40, 100, 135},   {0, 0, 0, 0, 25, 85, 130},
               {0, 0, 0, 0, 0, 60, 115},      {0, 0, 0, 0, 0, 0, 50}};
int up(int x) {
  if (x % 50) x = x / 100 * 100 + 50;
  return x;
}
int ch(int t) { return 1050 <= t && t <= 1170; }
int main() {
  for (; scanf("%d", &S), S;) {
    scanf("%d%d%d%d%d", &Ts[0], &Ts[1], &G, &Tg[0], &Tg[1]);
    S--;
    G--;
    if (S > G) S ^= G ^= S ^= G;
    for (d = 0, i = S + 1; i <= G; i++) d += D[i];
    r = p[S][G] * 10;
    if (d <= 40 && (ch(Ts[0] * 60 + Ts[1]) || ch(Tg[0] * 60 + Tg[1])))
      r = up(r / 2);
    printf("%d\n", r);
  }
  return 0;
}
