#include <iostream>
#include <string>
#include <vector>
using namespace std;

int tolls[7][7] = {
    {  -1,  300,  500,  600,  700, 1350, 1650},
    { 300,   -1,  350,  450,  600, 1150, 1500},
    { 500,  350,   -1,  250,  400, 1000, 1350},
    { 600,  450,  250,   -1,  250,  850, 1300},
    { 700,  600,  400,  250,   -1,  600, 1150},
    {1350, 1150, 1000,  850,  600,   -1,  500},
    {1650, 1500, 1350, 1300, 1150,  500,   -1}
};
int dist[7][7] = {
    {-1,  6, 13, 18, 23, 43, 58},
    { 6, -1,  7, 12, 17, 37, 52},
    {13,  7, -1,  5, 10, 30, 45},
    {18, 12,  5, -1,  5, 25, 40},
    {23, 17, 10,  5, -1, 20, 35},
    {43, 37, 30, 25, 20, -1, 15},
    {58, 52, 45, 40, 35, 15, -1}
};

int main() {
    int ic1, h1, m1, ic2, h2, m2;
    while (cin >> ic1, ic1) {
        cin >> h1 >> m1 >> ic2 >> h2 >> m2;
        --ic1; --ic2;
        bool half = false;
        int start = 17 * 60 + 30;
        int end = 19 * 60 + 30;
        int t1 = h1 * 60 + m1;
        int t2 = h2 * 60 + m2;
        if (dist[ic1][ic2] <= 40) {
            if ((start <= t1 && t1 <= end) || (start <= t2 && t2 <= end)) {
                half = true;
            }
        }
        int res = tolls[ic1][ic2];
        if (half) {
            if (tolls[ic1][ic2] % 100 == 50) res += 50;
            res /= 2;
        }
        cout << res << endl;
    }
    return 0;
}