#include <bits/stdc++.h>
using namespace std;
int s, sh, sm, g, gh, gm,
    ng[][2] = {{0, 5}, {0, 6}, {1, 6}, {2, 6}},
    t[][7] = {
        {0, 300, 500, 600, 700, 1350, 1650}, {0, 0, 350, 450, 600, 1150, 1500},
        {0, 0, 0, 250, 400, 1000, 1350},     {0, 0, 0, 0, 250, 850, 1300},
        {0, 0, 0, 0, 0, 600, 1150},          {0, 0, 0, 0, 0, 0, 500}};
int main() {
  while (cin >> s && s) {
    cin >> sh >> sm >> g >> gh >> gm;
    s--;
    g--;
    if (s > g) swap(s, g);
    int st = sh * 60 + sm, gt = gh * 60 + gm;
    int ok = 1;
    for (int i = 0; i < 4; i++)
      if (s == ng[i][0] && g == ng[i][1]) ok = 0;
    if (ok && (st <= 1050 && gt >= 1050 && gt <= 1170 ||
               st >= 1050 && st <= 1170 && gt >= 1170))
      cout << (t[s][g] / 50 + 1) / 2 * 50 << endl;
    else
      cout << t[s][g] << endl;
  }
}
