#include<iostream>
#define N 7

using namespace std;

int cost[N][N];
int dis[N][N];

void InitCost(){
  for(int i=0; i<N; i++) cost[i][i] = 0;

  cost[0][1] = 300;
  cost[0][2] = 500;
  cost[0][3] = 600;
  cost[0][4] = 700;
  cost[0][5] = 1350;
  cost[0][6] = 1650;

  cost[1][2] = 350;
  cost[1][3] = 450;
  cost[1][4] = 600;
  cost[1][5] = 1150;
  cost[1][6] = 1500;

  cost[2][3] = 250;
  cost[2][4] = 400;
  cost[2][5] = 1000;
  cost[2][6] = 1350;

  cost[3][4] = 250;
  cost[3][5] = 850;
  cost[3][6] = 1300;

  cost[4][5] = 600;
  cost[4][6] = 1150;

  cost[5][6] = 500;

  cost[1][0] = 300;
  cost[2][0] = 500;
  cost[3][0] = 600;
  cost[4][0] = 700;
  cost[5][0] = 1350;
  cost[6][0] = 1650;

  cost[2][1] = 350;
  cost[3][1] = 450;
  cost[4][1] = 600;
  cost[5][1] = 1150;
  cost[6][1] = 1500;

  cost[3][2] = 250;
  cost[4][2] = 400;
  cost[5][2] = 1000;
  cost[6][2] = 1350;

  cost[4][3] = 250;
  cost[5][3] = 850;
  cost[6][3] = 1300;

  cost[5][4] = 600;
  cost[6][4] = 1150;

  cost[6][5] = 500;
}

void InitDistance(){
  for(int i=0; i<N; i++) dis[i][i] = 0;

  dis[0][1] = 6;
  dis[0][2] = 13;
  dis[0][3] = 18;
  dis[0][4] = 23;
  dis[0][5] = 43;
  dis[0][6] = 58;

  dis[1][2] = 7;
  dis[1][3] = 12;
  dis[1][4] = 17;
  dis[1][5] = 37;
  dis[1][6] = 52;

  dis[2][3] = 5;
  dis[2][4] = 10;
  dis[2][5] = 30;
  dis[2][6] = 45;

  dis[3][4] = 5;
  dis[3][5] = 25;
  dis[3][6] = 40;

  dis[4][5] = 20;
  dis[4][6] = 35;

  dis[5][6] = 15;

  dis[1][0] = 6;
  dis[2][0] = 13;
  dis[3][0] = 18;
  dis[4][0] = 23;
  dis[5][0] = 43;
  dis[6][0] = 58;

  dis[2][1] = 7;
  dis[3][1] = 12;
  dis[4][1] = 17;
  dis[5][1] = 37;
  dis[6][1] = 52;

  dis[3][2] = 5;
  dis[4][2] = 10;
  dis[5][2] = 30;
  dis[6][2] = 45;

  dis[4][3] = 5;
  dis[5][3] = 25;
  dis[6][3] = 40;

  dis[5][4] = 20;
  dis[6][4] = 35;

  dis[6][5] = 15;
}

int WhatTime(int h, int m){
  if(h < 17 || (h == 17 && m < 30)) return -1;
  if(h > 19 || (h == 19 && m > 30)) return 1;
  return 0;
}

int HighwayToll(int n, int h1, int m1, int m, int h2, int m2){
  int ans;
  ans = cost[n][m];

  if(dis[n][m] <= 40){
    if((WhatTime(h1, m1) == 0 && WhatTime(h2, m2) == 0) ||
       (WhatTime(h1, m1) == -1 && WhatTime(h2, m2) == 0) ||
       (WhatTime(h1, m1) == 0 && WhatTime(h2, m2) == 1)){
      ans /= 2;
      if(ans%50 != 0) ans = ans - ans%50 + 50;
    }
  }
  return ans;
}

int main(){
  int n, m;
  int h1, m1, h2, m2;

  InitCost();
  InitDistance();

  while(1){
    cin >> n;
    if(n == 0) break;
    cin >> h1 >> m1;
    cin >> m;
    cin >> h2 >> m2;
    --n;
    --m;
    cout << HighwayToll(n, h1, m1, m, h2, m2) << endl;
  }
  return 0;
}