#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 1<<30

int main() {
  int table[7][7] = {{ 0,300,500,600,700,1350,1650}
                     ,{6,0,350,450,600,1150,1500}
                     ,{13,7,0,250,400,1000,1350}
                     ,{18,12,5,0,250,850,1300}
                     ,{23,17,10,5,0,600,1150}
                     ,{43,37,30,25,20,0,500}
                     ,{58,52,45,40,35,15,0}};
  int ss = 17*60+30;
  int tt = 19*60+30;
  int s, sh, sm, t, th, tm;
  while(cin >> s, s) {
    cin >> sh >> sm >> t >> th >> tm;
    s--;t--;
    if (s>t) swap(s,t);
    int ans = table[s][t];

    int sss = sh*60+sm;
    int ttt = th*60+tm;
    if (table[t][s] <= 40) {
      if (ss<=sss&&sss<=tt || ss<=ttt&&ttt<=tt) {
        ans /= 2;
        ans = (ans + 49) / 50 * 50;
      }
    }
    cout << ans << endl;
  }
}