#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = MOD + 1;
const long double EPS = 1e-12;
template <class T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  int H, A, D, S;
  cin >> H >> A >> D >> S;
  long long ans = 0;
  vector<pair<int, int> > v;
  for (int i = (int)(0); i < (int)(n); i++) {
    int h, a, d, s;
    cin >> h >> a >> d >> s;
    int dmg = max(0, a - D);
    if (S > s) {
      H += dmg;
      ans -= dmg;
    }
    int gdmg = max(0, A - d);
    if (dmg > 0 && gdmg == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (dmg == 0) continue;
    v.emplace_back((h + gdmg - 1) / gdmg, dmg);
  }
  sort(v.begin(), v.end(),
       [](const pair<int, int> &a, const pair<int, int> &b) {
         return (long double)a.first / a.second <
                (long double)b.first / b.second;
       });
  long long sum = 0;
  for (int i = (int)(0); i < (int)(v.size()); i++) {
    sum += v[i].first;
    ans += sum * v[i].second;
  }
  if (ans >= H)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
