#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int hh, ah, dh, sh;
  cin >> hh >> ah >> dh >> sh;
  pair<int, int> p[40000];
  long long dmg = 0;
  int INF = 1 << 30;
  long long dpt = 0;
  for (int i = 0; i < n; i++) {
    int h, a, d, s;
    cin >> h >> a >> d >> s;
    int of = max(0, a - dh);
    int df = max(0, ah - d);
    if (s > sh) {
      dmg += of;
      if (dmg >= hh) {
        cout << -1 << endl;
        return 0;
      }
    }
    dpt += of;
    p[i] = make_pair(of, df ? (h + df - 1) / df : INF);
  }
  sort(p, p + n, [](pair<int, int> a, pair<int, int> b) {
    return a.first * 1. / a.second > b.first * 1. / b.second;
  });
  for (int i = 0; i < n; i++) {
    if (p[i].second == 0) {
      if (p[i].first > 0) {
        cout << -1 << endl;
        return 0;
      }
    } else {
      dmg += dpt * p[i].second - p[i].first;
      if (dmg >= hh) {
        cout << -1 << endl;
        return 0;
      }
      dpt -= p[i].first;
    }
  }
  cout << dmg << endl;
}
