#include <bits/stdc++.h>
using namespace std;
int main() {
  int table[7][7] = {
      {0, 300, 500, 600, 700, 1350, 1650}, {6, 0, 350, 450, 600, 1150, 1500},
      {13, 7, 0, 250, 400, 1000, 1350},    {18, 12, 5, 0, 250, 850, 1300},
      {23, 17, 10, 5, 0, 600, 1150},       {43, 37, 10, 5, 0, 500},
      {58, 52, 45, 40, 35, 15, 0}};
  int ss = 17 * 60 + 30;
  int tt = 19 * 60 + 30;
  int s, sh, sm, t, th, tm;
  while (cin >> s, s) {
    cin >> sh >> sm >> t >> th >> tm;
    s--;
    t--;
    if (s > t) swap(s, t);
    int ans = table[s][t];
    int sss = sh * 60 + sm;
    int ttt = th * 60 + tm;
    if (table[t][s] <= 40) {
      if (ss <= sss && sss <= tt || ss <= ttt && ttt <= tt) {
        ans /= 2;
        ans = (ans + 49) / 50 * 50;
      }
    }
    cout << ans << endl;
  }
}
