#include <bits/stdc++.h>
using namespace std;
struct info {
  int h, a;
  double rate;
  info(int h, int a, int r) : h(h), a(a), rate(r) {}
};
bool comp(const info& a, const info& b) { return a.rate > b.rate; }
int main() {
  int n;
  cin >> n;
  long long int h, a, d, s;
  cin >> h >> a >> d >> s;
  vector<info> e;
  long long int sum = 0, damage = 0;
  for (int i = 0; i < n; i++) {
    int eh, ea, ed, es;
    cin >> eh >> ea >> ed >> es;
    ea -= d;
    if (ea <= 0) continue;
    if (es > s) {
      damage += ea;
    }
    if (a - ed <= 0) {
      h = 0;
      continue;
    }
    eh = (eh + (a - ed) - 1) / (a - ed);
    e.push_back(info(eh, ea, (double)ea / eh));
    sum += ea;
  }
  if (h <= 0) {
    cout << -1 << endl;
    return 0;
  }
  sort(e.begin(), e.end(), comp);
  for (int i = 0; i < (int)e.size(); i++) {
    int limit = (h - damage) / sum;
    if (e[i].h > limit) {
      h = 0;
      break;
    }
    damage += sum * (e[i].h - 1);
    sum -= e[i].a;
    damage += sum;
  }
  if (h <= damage) {
    cout << -1 << endl;
  } else {
    cout << damage << endl;
  }
  return 0;
}
