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