#include <bits/stdc++.h>
int x, y, h1, m1, h2, m2, t1, t2;
int table[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}};
void swap(int* x, int* y) {
  int z;
  z = *x;
  *x = *y;
  *y = z;
}
int main() {
  while (scanf("%d", &x) * x) {
    scanf("%d %d %d %d %d", &h1, &h2, &y, &h2, &m2);
    if (x > y) swap(&x, &y);
    x--;
    y--;
    t1 = h1 * 60 + m1;
    t2 = h2 * 60 + m2;
    if (table[y][x] <= 41 && ((17 * 60 + 30 <= t1 && t1 <= 19 * 60 + 30) ||
                              (17 * 60 + 30 <= t2 && t2 <= 19 * 60 + 30)))
      printf("%d\n", (table[x][y] / 2 + table[x][y] % 2) * 50);
    else
      printf("%d\n", table[x][y] * 50);
  }
  return 0;
}
