#include <bits/stdc++.h>
using namespace std;
struct enemy {
  int c, d, s;
  bool operator<(const enemy &e) const {
    return (long long)c * e.d < (long long)d * e.c;
  }
};
int main() {
  int n;
  scanf("%d", &n);
  int h[40001], a[40001], d[40001], s[40001];
  for (int i = 0; i < (n + 1); i++)
    scanf("%d%d%d%d", h + i, a + i, d + i, s + i);
  bool fail = false;
  for (int i = 0; i < (n); i++)
    if (a[0] <= d[i + 1]) fail = true;
  if (fail) {
    bool draw = true;
    for (int i = 0; i < (n); i++)
      if (d[0] < a[i + 1]) draw = false;
    if (draw)
      puts("0");
    else
      puts("-1");
    return 0;
  }
  int n_tmp = 0;
  enemy E[40000];
  for (int i = 0; i < (n); i++) {
    if (a[i + 1] <= d[0]) continue;
    E[n_tmp].c = ((h[i + 1] + a[0] - d[i + 1] - 1) / (a[0] - d[i + 1]));
    E[n_tmp].d = a[i + 1] - d[0];
    E[n_tmp].s = s[i + 1];
    n_tmp++;
  }
  n = n_tmp;
  sort(E, E + n);
  long long dmg = 0;
  for (int i = 0; i < (n); i++)
    if (s[0] < E[i].s) dmg += E[i].d;
  long long c_sum = 0;
  for (int i = 0; i < (n); i++) {
    c_sum += E[i].c;
    c_sum = min(c_sum, 2000000000LL);
    dmg += (c_sum - 1) * E[i].d;
    dmg = min(dmg, (long long)h[0]);
  }
  if (h[0] <= dmg)
    puts("-1");
  else
    printf("%lld\n", dmg);
  return 0;
}
