#include <bits/stdc++.h>
using namespace std;
int table[7][7] = {
    {-1, 300, 500, 600, 700, 1350, 1650}, {6, -1, 350, 450, 600, 1150, 1500},
    {13, 7, -1, 250, 400, 1000, 1350},    {18, 12, 5, -1, 250, 850, 1300},
    {23, 17, 10, 5, -1, 600, 1150},       {43, 37, 30, 25, 20, -1, 500},
    {58, 52, 45, 40, 35, 15, -1}};
int s, g, h1, m1, h2, m2;
int main() {
  while (cin >> s && s) {
    cin >> h1 >> m1 >> g >> h2 >> m2;
    g--;
    s--;
    if (s == g) {
      cout << 0 << endl;
      continue;
    }
    if (s > g) swap(s, g);
    int sum;
    if (table[g][s] <= 40) {
      if (h1 < 17 || (h1 == 17 && m1 <= 30)) {
        if (h2 < 19 || (h2 == 19 && m2 < 30)) {
          if (table[s][g] / 2 % 50 == 0) {
            sum = table[s][g] / 2 / 50 * 50;
          } else {
            sum = table[s][g] / 2 / 50 * 50 + 50;
          }
        } else {
          sum = table[s][g];
        }
      } else if (h1 > 17 || (h1 == 17 && m1 > 30)) {
        if (h2 > 19 || (h2 == 19 && m2 >= 30)) {
          if (table[s][g] / 2 % 50 == 0) {
            sum = table[s][g] / 2 / 50 * 50;
          } else {
            sum = table[s][g] / 2 / 50 * 50 + 50;
          }
        } else {
          sum = table[s][g];
        }
      }
    } else
      sum = table[s][g];
    cout << sum << endl;
  }
}
