#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
void solve() {
  int n;
  cin >> n;
  int64_t h, a, d, s;
  vector<int64_t> hs, as, ds, ss;
  vector<pair<int64_t, int64_t>> ps;
  vector<int> idx;
  cin >> h >> a >> d >> s;
  for (int i = (0); i < (n); ++i) {
    int64_t h_, a_, d_, s_;
    cin >> h_ >> a_ >> d_ >> s_;
    if (a - d_ <= 0) {
      cout << -1 << endl;
      return;
    }
    idx.push_back(i);
    ps.push_back(
        make_pair(max(a_ - d, int64_t(0)), h_ / (a - d_) - (s_ < s ? 1 : 0)));
    hs.push_back(h_);
    as.push_back(a_);
    ds.push_back(d_);
    ss.push_back(s_);
  }
  sort(idx.begin(), idx.end(), [&](int i, int j) {
    return ps[i].first * ps[i].second < ps[j].first * ps[j].second;
  });
  int64_t ans = 0, sum = 0;
  for (int i : idx) {
    ans += ps[i].first * ps[i].second + sum * (hs[i] / (a - ds[i]));
    sum += as[i] - d;
  }
  cout << (ans < h ? ans : -1) << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}
