#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

bool intime(int n)
{
	return 1730<=n && n<=1930;
}

pii getinfo(int src,int dst)
{
	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},
	};
	src--;dst--;
	if(src>dst) swap(src,dst);
	return mp(table[dst][src],table[src][dst]);
}

int main()
{
	
	for(int n1,h1,m1,n2,h2,m2;cin>>n1,n1;){
		cin>>h1>>m1>>n2>>h2>>m2;
		int t1=h1*100+m1,t2=h2*100+m2;
		pii info=getinfo(n1,n2);
		bool harf=(intime(t1)||intime(t2))&&info.first<=40;
		if(harf){
			info.second/=2;
			info.second=(info.second+49)/50*50;
		}
		cout<<info.second<<endl;
	}
	
	return 0;
}