#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <class T1, class T2>
void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
using Pi = pair<long long, long long>;
using Ti = tuple<long long, long long, long long>;
using vint = vector<long long>;
const long long inf = 1LL << 55;
const long long mod = 1e9 + 7;
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);
  long long n;
  cin >> n;
  vint h(n + 1), a(n + 1), d(n + 1), s(n + 1);
  for (long long i = (long long)(0); i < (long long)(n + 1); i++)
    cin >> h[i] >> a[i] >> d[i] >> s[i];
  vint id(n);
  iota((id).begin(), (id).end(), 1);
  auto cmp = [&](long long i, long long j) {
    long long e = (h[i] + a[0] - 1) / a[0];
    long long f = (h[j] + a[0] - 1) / a[0];
    long long g = max(0LL, a[i] - d[0]);
    long long h = max(0LL, a[j] - d[0]);
    return g * f > h * e;
  };
  sort((id).begin(), (id).end(), cmp);
  long long ans = 0;
  for (long long i = (long long)(0); i < (long long)(n); i++) {
    long long x = i + 1;
    if (s[0] > s[id[i]]) x--;
    ans += max(0LL, a[id[i]] - d[0]) * x;
  }
  if (ans < h[0])
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
