#include <bits/stdc++.h>
using namespace std;
struct po {
  long long h, a, d, s;
};
long long n;
po A[40001];
long long hp, a, d, s, sum;
bool compare(po x, po y) {
  if (a - x.d <= 0) return 0;
  if (a - y.d <= 0) return 1;
  long long cnt1 = 1 + (x.h - 1) / (a - x.d), cnt2 = 1 + (y.h - 1) / (a - y.d);
  long long X = cnt1 * (x.a - d) + (cnt1 + cnt2) * (y.a - d);
  long long Y = cnt2 * (y.a - d) + (cnt1 + cnt2) * (x.a - d);
  return X < Y;
}
long long solve() {
  sort(A, A + n, compare);
  for (int i = 0; i < n; i++) {
    po t = A[i];
    if (a - t.d <= 0) return -1;
    long long cost = ((t.h - 1) / (a - t.d)) * sum;
    sum -= t.a - d;
    hp -= (cost + sum);
    if (hp <= 0) return -1;
  }
  if (hp <= 0) return -1;
  return hp;
}
int main() {
  cin >> n;
  cin >> hp >> a >> d >> s;
  long long ofs = 0;
  for (int i = 0; i < n; i++) {
    po &t = A[i];
    cin >> t.h >> t.a >> t.d >> t.s;
    if (t.a - d < 0) {
      i--;
      continue;
    };
    if (t.s > s) ofs += (t.a - d);
    sum += (t.a - d);
  }
  long long H = hp;
  hp -= ofs;
  long long ans = H - solve();
  if (H < ans)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
