#include <bits/stdc++.h>
using namespace std;
int s[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, 30, 25, 20, 0, 500},
    {58, 52, 45, 40, 35, 15, 0}};
int main() {
  int sc, sh, sm, ec, eh, em;
  while (cin >> sc && sc) {
    cin >> sh >> sm >> ec >> eh >> em;
    int an = s[sc - 1][ec - 1], S = sh * 100 + sm, E = eh * 100 + em;
    if (S > E) E += 2400;
    if (s[ec - 1][sc - 1] <= 40 &&
        ((1930 < S && E < 1730 + 2400 && E < 1930 + 2400) ||
         (S < 1730 && 1730 < E && E < 1930) ||
         (1730 < S && S < 1930 && 1930 < E && E < 1730 + 2400) ||
         (S == 1730 && 1730 < E && E < 1930) ||
         (S == 1930 && 1930 < E && E < 1730 + 2400) || (E == 1730 && S < 0) ||
         (E == 1930 && 1730 < S && S < 1930) ||
         (E == 1730 + 2400 && 1930 < S && S < 1730 + 2400)))
      an /= 2;
    if (an % 50) an -= an % 50, an += 50;
    cout << an << endl;
  }
  return 0;
}
