#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int v[7][7]={{-1,300,500,600,700,1350,1650},
                 {6,-1,350,450,600,1150,1500},
                 {13,7,-1,250,400,1000,1350},
                 {18,12,5,-1,250,850,1300},
                 {23,17,10,5,-1,600,1150},
                 {43,37,30,25,20,-1,500},
                 {58,52,45,40,35,15}};
    int a;
    while(cin >> a, a){
        int h1, m1, b, h2, m2;
        cin >> h1 >> m1 >> b >> h2 >> m2;
        a--; b--;
        if(a > b) swap(a, b);
        int p = h1*60+m1, q = h2*60+m2;
        if(v[b][a] <= 40 && ((1050 <= p && p <= 1170) || (1050 <= q && q <= 1170))){
            cout << (v[a][b]/2+49)/50*50 << "\n";
        }else {
            cout << v[a][b] << "\n";
        }
    }
    return 0;
}
