#include <bits/stdc++.h>
#define FOR(i,k,n) for(int i=(k);i<(int)(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) begin(x),end(x)

using namespace std;

int main() {
  int64_t n;
  cin>>n;
  using T = tuple<int64_t,int64_t,int64_t,int64_t>;
  vector<T> vc;
  int64_t h0,a0,d0,s0;
  cin>>h0>>a0>>d0>>s0;
  REP(i,n) {
    int64_t h,a,d,s;
    cin>>h>>a>>d>>s;
    vc.emplace_back(max(a-d0,0),s,max(0,a0-d),h);
  }
  sort(ALL(vc),greater<T>());
  int64_t turns = 0;
  bool imp = false;
  int64_t damage = 0;
  for (T t:vc) {
    int64_t d,s,a,h;
    tie(d,s,a,h) = t;
    if (a == 0 && d != 0) {
      imp = true;
      break;
    } else {
      turns += (h + a-1) / a;
      if (s < s0) {
        damage += (turns-1) * d;
      } else {
        damage += turns * d;
      }
    }
  }
  if (imp || damage >= h0) {
    cout << -1 << endl;
  } else {
    cout << damage << endl;
  }
  return 0;
}