#include <bits/stdc++.h>
using namespace std;
struct Chara {
  long long h, a, d, s, t;
  bool operator<(const Chara& c) const { return c.t * a < t * c.a; }
};
long long solve(int n) {
  Chara me;
  cin >> me.h >> me.a >> me.d >> me.s;
  vector<Chara> rival(n);
  for (Chara& c : rival) {
    cin >> c.h >> c.a >> c.d >> c.s;
    c.a = max(0LL, c.a - me.d);
  }
  for (const Chara& c : rival) {
    if (me.a <= c.d) {
      return -1;
    }
  }
  for (Chara& c : rival) {
    c.t = (c.h + me.a - c.d - 1) / (me.a - c.d);
  }
  long long sum = 0;
  for (const Chara& c : rival) {
    if (c.s < me.s) {
      sum -= c.a;
    }
  }
  sort(rival.begin(), rival.end());
  long long time = 0;
  for (const Chara& c : rival) {
    time += c.t;
    sum += c.a * time;
  }
  if (sum >= me.h) {
    return -1;
  } else {
    return sum;
  }
}
int main() {
  int n;
  while (cin >> n) {
    cout << solve(n) << endl;
  }
  return 0;
}
