#include <bits/stdc++.h>
int main(int argc, char **argv) {
  const int toll_t[7][7] = {{0, 300, 500, 600, 700, 1350, 1650},
                            {300, 0, 350, 450, 600, 1150, 1500},
                            {500, 350, 0, 250, 400, 1000, 1350},
                            {600, 450, 250, 0, 250, 850, 1300},
                            {700, 600, 400, 250, 0, 600, 1150},
                            {1350, 1150, 1000, 850, 600, 0, 500},
                            {1650, 1500, 1350, 1300, 1150, 500, 0}};
  const int dist_t[7][7] = {
      {0, 6, 13, 18, 23, 43, 58}, {6, 0, 7, 12, 17, 37, 52},
      {13, 7, 0, 5, 10, 30, 45},  {18, 12, 5, 0, 5, 25, 40},
      {23, 17, 10, 5, 0, 20, 35}, {43, 37, 30, 25, 20, 0, 15},
      {58, 52, 45, 40, 35, 15, 0}};
  const int time1 = 17 * 60 + 30;
  const int time2 = 19 * 60 + 30;
  while (true) {
    int d;
    scanf("%d", &d);
    if (d == 0) break;
    d--;
    int h, m;
    scanf("%d%d", &h, &m);
    int t_src = 60 * h + m;
    int a;
    scanf("%d", &a);
    a--;
    scanf("%d%d", &h, &m);
    int t_dest = 60 * h + m;
    int dist = dist_t[d][a];
    int toll;
    if (((t_src <= time1 && time1 <= t_dest && t_dest < time2) ||
         (time1 < t_src && t_src <= time2 && time2 <= t_dest)) &&
        dist <= 40) {
      double b = toll_t[d][a] / (2.0 * 50.0);
      toll = ceil(b) * 50;
    } else
      toll = toll_t[d][a];
    printf("%d\n", toll);
  }
  return 0;
}
