#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int h, a, d, s;
  cin >> h >> a >> d >> s;
  vector<int> eh(n), ea(n), ed(n), es(n);
  vector<pair<long double, int>> ss(n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> eh[i] >> ea[i] >> ed[i] >> es[i];
    if (es[i] > s) ans += ((ea[i] - d) > (0) ? (ea[i] - d) : (0));
    if (((a - ed[i]) > (0) ? (a - ed[i]) : (0)) == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (((ea[i] - d) > (0) ? (ea[i] - d) : (0)) == 0)
      ss[i] = {1000000000000000000, i};
    else
      ss[i] = {((eh[i] + ((a - ed[i]) > (0) ? (a - ed[i]) : (0)) - 1) /
                    ((a - ed[i]) > (0) ? (a - ed[i]) : (0)) -
                1) /
                   ((ea[i] - d) > (0) ? (ea[i] - d) : (0)),
               i};
  }
  sort((ss).begin(), (ss).end());
  int turn = 0;
  int id;
  cout << ans << endl;
  for (int i = 0; i < n; i++) {
    id = ss[i].second;
    turn += (eh[id] + ((a - ed[id]) > (0) ? (a - ed[id]) : (0)) - 1) /
                ((a - ed[id]) > (0) ? (a - ed[id]) : (0)) -
            1;
    ans += turn * ((ea[id] - d) > (0) ? (ea[id] - d) : (0));
    turn++;
  }
  if (ans >= h)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
