#include <bits/stdc++.h>
using namespace std;
int dist[] = {0, 6, 13, 18, 23, 43, 58};
int cost[][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 main() {
  int s, g;
  while (cin >> s, s) {
    int h, m;
    bool half = false;
    cin >> h >> m;
    if (17 * 60 + 30 <= h * 60 + m && h * 60 + m <= 18 * 60 + 30) half = true;
    cin >> g >> h >> m;
    if (17 * 60 + 30 <= h * 60 + m && h * 60 + m <= 18 * 60 + 30) half = true;
    half &= abs(dist[s - 1] - dist[g - 1]) <= 40;
    if (half)
      cout << ((cost[s - 1][g - 1] / 50) + 1) / 2 * 50 << endl;
    else
      cout << cost[s - 1][g - 1] << endl;
  }
}
