#include <bits/stdc++.h>
using namespace std;
int n, h[40005], a[40005], d[40005], s[40005];
vector<pair<pair<int, int>, int> > M;
bool cmp(const pair<pair<int, int>, int> &a,
         const pair<pair<int, int>, int> &b) {
  return 1LL * a.first.first * b.first.second <
         1LL * b.first.first * a.first.second;
}
int main() {
  cin >> n;
  for (int i = 0; i <= n; i++) cin >> h[i] >> a[i] >> d[i] >> s[i];
  long long C = 0;
  for (int i = 1; i <= n; i++) {
    if (a[0] <= d[i]) {
      puts("-1");
      return 0;
    }
    if (a[i] <= d[0]) {
      continue;
    }
    C += (a[i] - d[0]);
    M.push_back(make_pair(
        make_pair((h[i] + a[0] - d[i] - 1) / (a[0] - d[i]), a[i] - d[0]), i));
  }
  sort(M.begin(), M.end(), cmp);
  long long cur = 0;
  for (int i = 0; i < M.size(); i++) {
    int m = M[i].second;
    int T = (h[m] + a[0] - d[m] - 1) / (a[0] - d[m]);
    cur += 1LL * T * C;
    if (s[m] < s[0]) cur -= (a[m] - d[0]);
    if (cur >= h[0]) {
      puts("-1");
      return 0;
    }
    C -= (a[m] - d[0]);
  }
  printf("%lld\n", cur);
}
