#include <bits/stdc++.h>
using namespace std;
int main() {
  for (int s; cin >> s, s;) {
    int sh, sm;
    int t, th, tm;
    cin >> sh >> sm >> t >> th >> tm;
    s--;
    t--;
    int f[7][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},
                   {0, 0, 0, 0, 0, 0, 0}};
    int d[7][7] = {{0, 0, 0, 0, 0, 0, 0},    {6, 0, 0, 0, 0, 0, 0},
                   {13, 7, 0, 0, 0, 0, 0},   {18, 12, 5, 0, 0, 0, 0},
                   {23, 17, 10, 5, 0, 0, 0}, {43, 37, 30, 25, 20, 0, 0},
                   {58, 52, 45, 40, 35, 15}};
    int sum = max(f[s][t], f[t][s]);
    if (!((((sh)*60 + (sm)) < ((17) * 60 + (30)) &&
           ((19) * 60 + (30)) < ((th)*60 + (tm))) ||
          ((sh)*60 + (sm)) > ((19) * 60 + (30)) ||
          ((th)*60 + (tm)) < ((17) * 60 + (30))) &&
        max(d[s][t], d[s][t]) < 40) {
      sum = sum / 2 + ((sum % 100 == 50) ? 25 : 0);
    }
    cout << sum << endl;
  }
  return 0;
}
