#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, H[40000], A[40000], D[40000], S[40000];
  scanf("%lld", &N);
  for (int i = 0; i < N + 1; i++) {
    scanf("%lld %lld %lld %lld", H + i, A + i, D + i, S + i);
  }
  long long ret = 0, turn = 0;
  vector<pair<long long, long long> > xs;
  for (int i = 1; i <= N; i++) {
    long long GetDamage = A[i] - D[0];
    long long AddDamage = A[0] - D[i];
    if (GetDamage <= 0) continue;
    if (S[i] > S[0]) ret += GetDamage;
    if (AddDamage <= 0) {
      puts("-1");
      return (0);
    }
    xs.emplace_back((H[i] + AddDamage - 1) / AddDamage, GetDamage);
    turn += GetDamage;
  }
  sort(begin(xs), end(xs),
       [](const pair<long long, long long> &a,
          const pair<long long, long long> &b) {
         return (a.first * b.second < b.first * a.second);
       });
  for (auto &p : xs) {
    ret += turn * p.first - p.second;
    turn -= p.second;
  }
  if (ret >= H[0])
    puts("-1");
  else
    printf("%lld\n", ret);
}
