#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

bool in(int h, int m){
    if((h == 17 && m >=30)||(h == 18)||(h == 19 && m <=30)) return true;
    return false;
}

signed main(){
    int s, t, sh, sm, th, tm;
    int d[49] = {0, 30, 50, 60, 70, 135, 165,
                6, 0, 35, 45, 60, 115, 150,
                13, 7, 0, 25, 40, 100, 135,
                18, 12, 5, 0, 25, 85, 130,
                23, 17, 10, 5, 0, 60, 115,
                43, 37, 30, 25, 20, 0, 50,
                58, 52, 45, 40, 35, 15, 0};

    while(cin >> s, s){
        cin >> sh >> sm >> t >> th >> tm;
        s--; t--;
        if(s > t) swap(s, t);
        int ans = d[s*7 + t] * 10;
        if((in(sh, sm)||in(th, tm)) && d[t*7 + s] <= 40) ans /= 2;
        if(ans % 50 != 0) ans = (ans / 50 + 1)*50;
        cout << ans << endl;
    }
}