#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#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 <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int my[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,800,600,0,500},
					{1650,1500,1350,1300,1150,500,0}};

int ds[7][7] = {{0,6,13,18,23,43,58},
					{6,0,7,12,17,37,52},
					{13,7,0,5,10,30,45},
					{18,12,5,0,5,25,40},
					{23,17,10,5,0,20,35},
					{43,37,30,25,20,0,15},
					{58,52,45,40,35,15,0}};

bool half(int len,int l,int r){
	if(len > 40) return false;
	const int x = 17*60+30,y=19*60+30;
	return (x <= l && l <= y) || (x <= r && r <= y);
}

int main(){
	int n;
	while(cin>>n,n){
		int nt; {int a,b; cin>>a>>b; nt = a * 60 + b; }
		int m; cin>>m;
		int mt; {int a,b; cin>>a>>b; mt = a * 60 + b; }
		int len = ds[n-1][m-1];
		bool h = half(len,nt,mt);
		int mo = my[n-1][m-1];
		if(h) mo = ((mo/50+1)/2) * 50;
		cout << mo << endl;
	}

	return 0;
}