#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

int main()
{
  int64 N, H[40001], A[40001], D[40001], S[40001];

  scanf("%lld", &N);
  for(int i = 0; i < N + 1; i++) {
    scanf("%lld %lld %lld %lld", H + i, A + i, D + i, S + i);
  }

  int64 ret = 0;
  int64 turn = 0;
  vector< pair< int64, int64 > > xs;
  for(int i = 1; i <= N; i++) {
    int64 GetDamage = A[i] - D[0];
    int64 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< int64, int64 > &a,
                              const pair< int64, int64 > &b)
  {
    return (a.first * b.second < b.first * a.second);
  });

  for(auto &p : xs) {
    ret += turn * p.first - p.second;
    if(ret >= H[0]) {
      puts("-1");
      return (0);
    }
    turn -= p.second;
  }

  printf("%lld\n", max(-1LL, ret));
}