#include<iostream>
using namespace std;

int main(){
  int f,t;
  int dis[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},
		    {58,52,45,40,35,15, 0} };
  int fee[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} };
  int h,m;
  bool flag;
  int ans;

  while(1){
    cin >> f;
    if(!f)break;
    f--;

    flag = false;
    cin >> h >> m;
    if(h==17 && m>=30)flag = true;
    else if(h == 18)flag = true;
    else if(h==19 && m<=30)flag = true;

    cin >> t;
    t--;
    cin >> h >> m;
    if(h==17 && m>=30)flag = true;
    else if(h == 18)flag = true;
    else if(h==19 && m<=30)flag = true;

    ans = fee[f][t];
    if(flag && dis[f][t]<=40){
      ans /= 2;
      if(ans != (ans/50)*50)ans = (ans/50 + 1)*50;
    }
    cout << ans << endl;
  }
}