#include <bits/stdc++.h>
using namespace std;
int main() {
  pair<int, int> hyou[8][8];
  hyou[1][2] = hyou[2][1] = pair<int, int>(6, 300);
  hyou[1][3] = hyou[3][1] = pair<int, int>(13, 500);
  hyou[1][4] = hyou[4][1] = pair<int, int>(18, 600);
  hyou[1][5] = hyou[5][1] = pair<int, int>(23, 700);
  hyou[1][6] = hyou[6][1] = pair<int, int>(43, 1350);
  hyou[1][7] = hyou[7][1] = pair<int, int>(58, 1650);
  hyou[2][3] = hyou[3][2] = pair<int, int>(7, 350);
  hyou[2][4] = hyou[4][2] = pair<int, int>(12, 450);
  hyou[2][5] = hyou[5][2] = pair<int, int>(17, 600);
  hyou[2][6] = hyou[6][2] = pair<int, int>(37, 1150);
  hyou[2][7] = hyou[7][2] = pair<int, int>(52, 1500);
  hyou[3][4] = hyou[4][3] = pair<int, int>(5, 250);
  hyou[3][5] = hyou[5][3] = pair<int, int>(10, 400);
  hyou[3][6] = hyou[6][3] = pair<int, int>(30, 1000);
  hyou[3][7] = hyou[7][3] = pair<int, int>(45, 1350);
  hyou[4][5] = hyou[5][4] = pair<int, int>(5, 250);
  hyou[4][6] = hyou[6][4] = pair<int, int>(25, 850);
  hyou[4][7] = hyou[7][4] = pair<int, int>(40, 1300);
  hyou[5][6] = hyou[6][5] = pair<int, int>(20, 600);
  hyou[5][7] = hyou[7][5] = pair<int, int>(35, 1150);
  hyou[6][7] = hyou[7][6] = pair<int, int>(15, 500);
  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    int sh, sm;
    cin >> sh >> sm;
    int m;
    cin >> m;
    int gh, gm;
    cin >> gh >> gm;
    if (sh == 18 || gh == 18) {
      if (hyou[n][m].first <= 40) {
        int p = hyou[n][m].second;
        p /= 2;
        if (p % 100 == 25) {
          p += 25;
        }
        cout << p << endl;
      } else {
        cout << hyou[n][m].second << endl;
      }
    } else if (sh == 17 || gh == 17) {
      int p = hyou[n][m].second;
      if (sh == 17) {
        if (sm >= 30) {
          if (hyou[n][m].first <= 40) {
            p /= 2;
            if (p % 100 == 25) {
              p += 25;
            }
          }
        } else if (gh == 17) {
          if (gm >= 30) {
            if (hyou[n][m].first <= 40) {
              p /= 2;
              if (p % 100 == 25) {
                p += 25;
              }
            }
          }
        } else if (gh == 19) {
          if (gm <= 30) {
            if (hyou[n][m].first <= 40) {
              p /= 2;
              if (p % 100 == 25) {
                p += 25;
              }
            }
          }
        }
      } else {
        if (gm >= 30) {
          if (hyou[n][m].first <= 40) {
            p /= 2;
            if (p % 100 == 25) {
              p += 25;
            }
          }
        } else if (sh == 19) {
          if (sm <= 30) {
            if (hyou[n][m].first <= 40) {
              p /= 2;
              if (p % 100 == 25) {
                p += 25;
              }
            }
          }
        }
      }
      cout << p << endl;
    } else if (sh == 19 || gh == 19) {
      int p = hyou[n][m].second;
      if (sh == 19) {
        if (sm <= 30) {
          if (hyou[n][m].first <= 40) {
            p /= 2;
            if (p % 100 == 25) {
              p += 25;
            }
          }
        } else if (gh == 19) {
          if (gm <= 30) {
            if (hyou[n][m].first <= 40) {
              p /= 2;
              if (p % 100 == 25) {
                p += 25;
              }
            }
          }
        }
      } else {
        if (gm <= 30) {
          if (hyou[n][m].first <= 40) {
            p /= 2;
            if (p % 100 == 25) {
              p += 25;
            }
          }
        }
      }
      cout << p << endl;
    } else {
      cout << hyou[n][m].second << endl;
    }
  }
  return 0;
}
