#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#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 mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int ck[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},
};

main(){
  int eic;
  while(cin>>eic,eic){
    int eh,em,tic,th,tm;
    cin>>eh>>em>>tic>>th>>tm;
    if(eic>tic)swap(eic,tic);

    --eic,--tic;
    int kyori=ck[tic][eic];
    if(((60*eh+em>=17*60+30 && 60*eh+em<=19*60+30) ||
	(60*th+tm>=17*60+30 && 60*th+tm<=19*60+30)) &&
       kyori<=40)cout<<(ck[eic][tic]/2+49)/50*50;
       else cout<<ck[eic][tic];
       cout<<endl;
  }
}