#include <iostream>
using namespace std;

const int c[][8] = {
    {0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 300, 500, 600, 700, 1350, 1650},
    {0, 6, 0, 350, 450, 600, 1150, 1500},
    {0, 13, 7, 0, 250, 400, 1000, 1350},
    {0, 18, 12, 5, 0, 250, 850, 1300},
    {0, 23, 17, 10, 5, 0, 600, 1150},
    {0, 43, 37, 30, 25, 20, 0, 500},
    {0, 58, 52, 45, 40, 35, 15, 0},
};
const int bt = 60 * 17 + 30;
const int lt = 60 * 19 + 30;

int main() {
    int s;
    while (cin >> s, s) {
        bool half = false;
        int hh, mm;
        cin >> hh >> mm;
        int time = 60 * hh + mm;
        half |= (bt <= time && time <= lt);
        int t;
        cin >> t >> hh >> mm;
        time = 60 * hh + mm;
        half |= (bt <= time && time <= lt);
        half &= (c[t][s] <= 40);
        int ans = c[s][t];
        if (half) {
            ans /= 2;
            if (ans % 50 != 0) {
                ans += 50 - (ans % 50);
            }
        }
        cout << ans << endl;
    }
}