#include <bits/stdc++.h>
using namespace std;
int charge(int in, int out);
int dist(int in, int out);
int checkhalf(int ih, int im, int oh, int om, int dis);
int half(int y);
int main() {
  int inic, inh, inm, outic, outh, outm, yen, kyori;
  while (1) {
    cin >> inic;
    if (inic == 0) break;
    cin >> inh >> inm >> outic >> outh >> outm;
    yen = charge(inic, outic);
    kyori = dist(inic, outic);
    if (checkhalf(inh, inm, outh, outm, kyori)) yen = half(yen);
    cout << yen << endl;
  }
  return 0;
}
int charge(int in, int out) {
  if (in == 1) {
    if (out == 2) return 300;
    if (out == 3) return 500;
    if (out == 4) return 600;
    if (out == 5) return 700;
    if (out == 6) return 1350;
    if (out == 7) return 1650;
  } else if (in == 2) {
    if (out == 1) return 300;
    if (out == 3) return 350;
    if (out == 4) return 450;
    if (out == 5) return 600;
    if (out == 6) return 1150;
    if (out == 7) return 1500;
  } else if (in == 3) {
    if (out == 1) return 500;
    if (out == 2) return 350;
    if (out == 4) return 250;
    if (out == 5) return 400;
    if (out == 6) return 1000;
    if (out == 7) return 1350;
  } else if (in == 4) {
    if (out == 1) return 600;
    if (out == 2) return 450;
    if (out == 3) return 250;
    if (out == 5) return 250;
    if (out == 6) return 850;
    if (out == 7) return 1300;
  } else if (in == 5) {
    if (out == 1) return 700;
    if (out == 2) return 600;
    if (out == 3) return 400;
    if (out == 4) return 250;
    if (out == 6) return 600;
    if (out == 7) return 1150;
  } else if (in == 6) {
    if (out == 1) return 1350;
    if (out == 2) return 1150;
    if (out == 3) return 1000;
    if (out == 4) return 850;
    if (out == 5) return 600;
    if (out == 7) return 500;
  } else if (in == 7) {
    if (out == 1) return 1650;
    if (out == 2) return 1500;
    if (out == 3) return 1350;
    if (out == 4) return 1300;
    if (out == 5) return 1150;
    if (out == 6) return 500;
  }
}
int dist(int in, int out) {
  if (in == 1) {
    if (out == 2) return 6;
    if (out == 3) return 13;
    if (out == 4) return 18;
    if (out == 5) return 23;
    if (out == 6) return 43;
    if (out == 7) return 58;
  } else if (in == 2) {
    if (out == 3) return 7;
    if (out == 4) return 12;
    if (out == 5) return 17;
    if (out == 6) return 37;
    if (out == 7) return 52;
  } else if (in == 3) {
    if (out == 4) return 5;
    if (out == 5) return 10;
    if (out == 6) return 30;
    if (out == 7) return 45;
  } else if (in == 4) {
    if (out == 5) return 5;
    if (out == 6) return 25;
    if (out == 7) return 40;
  } else if (in == 5) {
    if (out == 6) return 20;
    if (out == 7) return 35;
  } else if (in == 6) {
    if (out == 7) return 15;
  }
}
int checkhalf(int ih, int im, int oh, int om, int dis) {
  int flag1 = 0, flag2 = 0, flag3 = 0;
  if (dis > 40) return 0;
  if (ih < 17 || (ih == 17 && im <= 30)) {
    if ((oh == 19 && om < 30) || oh < 19) {
      flag1 = 1;
    }
  }
  if (ih > 17 || (ih == 17 && im > 30)) {
    if ((oh == 19 && om >= 30) || oh > 19) {
      flag2 = 1;
    }
  }
  if ((ih > 17 || (ih == 17 && im > 30)) &&
      ((oh == 19 && om < 30) || oh < 19)) {
    flag3 = 1;
  }
  if ((flag1 && flag2) || (!flag1 && !flag2) || flag3)
    return 0;
  else
    return 1;
}
int half(int y) {
  int temp = y;
  temp /= 2;
  if (temp % 50 != 0) {
    temp = temp + abs((temp % 50) - 50);
  }
  return temp;
}
