#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n, h[40001], a[40001], d[40001], s[40001], ans = 0;
  vector<pair<long long, long long> > vec;
  cin >> n;
  for (long long i = 0; i <= n; i++) {
    cin >> h[i] >> a[i] >> d[i] >> s[i];
    if (i) {
      if (a[0] - d[i] <= 0) {
        cout << -1 << endl;
        return 0;
      }
      if (s[i] > s[0]) {
        ans += max(a[i] - d[0], 0ll);
      }
      vec.push_back(
          pair<long long, long long>((h[i] - 1) / (a[0] - d[i]) + 1, i));
    }
  }
  long long sum = 0;
  sort(vec.begin(), vec.end());
  for (long long i = 0; i < n; i++) {
    if (ans >= h[0]) {
      cout << -1 << endl;
      return 0;
    }
    sum += vec[i].first;
    ans += (sum - 1) * max(a[vec[i].second] - d[0], 0ll);
  }
  if (ans >= h[0])
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
