#include "bits/stdc++.h"
using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define MP(a, b) make_pair(a, b)
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n, m;
vector<pi> s;
int dp[1024];
using namespace std;
using namespace std;
int main() {
 char c1[2],c2[2];
 int h,m,ic[2],f[2],i,k;
 int g[7][7]={{0,300,500,600,700,1350,1650}, {300,0,350,450,600,1150,1500}, {500,350,0,250,400,1000,1350},
 {600,450,250,0,250,850,1300}, {700,600,400,250,0,600,1150}, {1350,1150,1000,850,600,0,500},{1650,1500,1350,1300,1150,500,0}};
 while(true) {
     for (i=0;i<2;i++) {
         cin >> ic[i];
         if (ic[i]==0) return 0;
         ic[i]--;
         cin >> c1 >> c2;
         h=(c1[0]-'0')*10+(c1[1]-'0');
         m=(c2[0]-'0')*10+(c2[1]-'0');
         f[i]=(h==17 && m>29) || (h==18) || (h==19 && m<31);  
         }
     k=g[ic[0]][ic[1]];
     if(k<1350) 
         if (f[0]+f[1]>0) k=k % 100>0 ? k/2+25:k/2;
     cout << k << endl;
 }
return 0;
 }