#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int dis[7][7], cost[7][7];

void init() {
  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;
  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;
}

bool in_range(int h, int m) {
  if (h < 17 || h > 19) return false;
  if (h == 17 && m < 30) return false;
  if (h == 19 && m > 30) return false;
  return true;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  init();
  int d, a;
  while (cin >> d, d) {
    int hd, md, ha, ma;
    cin >> hd >> md >> a >> ha >> ma;
    d--, a--;
    if (d > a) swap(d, a);
    bool ok = (dis[d][a] <= 40);
    ok &= (in_range(hd, md) || in_range(ha, ma));
    cout << (ok ? (cost[d][a] / 2 + 50 - 1) / 50 * 50 : cost[d][a]) << endl;
  }

  return 0;
}
