#include <bits/stdc++.h>
using namespace std;
struct Data {
  int h, a, d, s;
  Data() {}
  Data(int h, int a, int d, int s) : h{h}, a{a}, d{d}, s{s} {}
  bool operator<(const Data& d) const { return s < d.s; }
};
istream& operator>>(istream& is, Data& d) {
  return is >> d.h >> d.a >> d.d >> d.s;
}
using ll = long long;
int solve(Data& M, vector<Data>& ene) {
  vector<pair<double, Data>> v;
  for (const Data& e : ene) {
    if (M.a <= e.d) return -1;
    int turn = ceil((double)e.h / (M.a - e.d));
    int damage = turn * (max(0, e.a - M.d));
    v.emplace_back((double)damage / turn, e);
  }
  sort(v.rbegin(), v.rend());
  ll res = 0, total_turn = 0;
  for (const auto& e : v) {
    Data d = e.second;
    int turn = ceil((double)d.h / (M.a - d.d));
    total_turn += turn;
    M.h -= e.first * (total_turn - (M.s > d.s));
    if (M.h <= 0) return -1;
    res += e.first * (total_turn - (M.s > d.s));
  }
  return res;
}
int main() {
  int N;
  cin >> N;
  Data M;
  cin >> M;
  vector<Data> ene(N);
  for (int i = 0; i < N; i++) {
    cin >> ene[i];
  }
  cout << solve(M, ene) << endl;
  return 0;
}
