#include <bits/stdc++.h>
using namespace std;
using LLI = long long int;
int n;
int order[114514];
LLI hs[114514];
LLI as[114514];
LLI ds[114514];
LLI ss[114514];
bool cmp(int a, int b) {
  LLI at = hs[a] / (as[0] - ds[a]) + (hs[a] % (as[0] - ds[a]) != 0);
  LLI bt = hs[b] / (as[0] - ds[b]) + (hs[b] % (as[0] - ds[b]) != 0);
  LLI aa = as[a] * (at + bt) + as[b] * (bt - (ss[0] > ss[b]));
  LLI ba = as[b] * (at + bt) + as[a] * (at - (ss[0] > ss[a]));
  return aa > ba;
}
int main() {
  scanf("%d", &n);
  bool imp = false;
  for (int i = 0; i <= n; i++) {
    scanf("%lld%lld%lld%lld", &hs[i], &as[i], &ds[i], &ss[i]);
    if (i >= 1) {
      order[i - 1] = i;
      if (as[0] <= ds[i]) imp = true;
    }
  }
  if (imp) {
    puts("-1");
    return 0;
  }
  sort(order, order + n, cmp);
  LLI turn = 0;
  LLI damage = 0;
  for (int i = 0; i < n; i++) {
    int a = order[i];
    LLI at = hs[a] / (as[0] - ds[a]) + (hs[a] % (as[0] - ds[a]) != 0);
    turn += at;
    damage += max(0LL, as[a] - ds[0]) * (turn - (ss[0] > ss[a]));
    if (damage >= hs[0]) imp = true;
  }
  if (imp) {
    puts("-1");
    return 0;
  }
  printf("%lld\n", damage);
}
