#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  Int n;
  cin >> n;
  n++;
  vector<Int> h(n), a(n), d(n), s(n);
  for (Int i = 0; i < n; i++) cin >> h[i] >> a[i] >> d[i] >> s[i];
  bool f = 1;
  for (Int i = 1; i < n; i++)
    if (d[0] - a[i] <= 0) f = 0;
  if (f) {
    cout << 0 << endl;
    return 0;
  }
  for (Int i = 1; i < n; i++)
    if (a[0] - d[i] <= 0 && a[i] - d[0] > 0) f = 1;
  if (f) {
    cout << -1 << endl;
    return 0;
  }
  auto get = [&](Int i, bool f = 1) {
    Int k = a[0] - d[i];
    if (k <= 0) return 0LL;
    Int res = (h[i] + k - 1) / k;
    if (f && s[i] < s[0]) res--;
    return res;
  };
  auto dm = [&](Int i) { return max(a[i] - d[0], 0LL); };
  vector<Int> x(n - 1);
  for (Int i = 1; i < n; i++) x[i - 1] = i;
  auto tmp = [&](Int p, Int q) {
    return get(p) * dm(p) + (get(p, 0) + get(q)) * dm(q);
  };
  sort(x.begin(), x.end(), [&](Int p, Int q) { return tmp(p, q) < tmp(q, p); });
  Int t = 0;
  Int ans = accumulate(x.begin(), x.end(), 0LL, [&](Int res, Int y) {
    if (res >= h[0]) return res;
    res += (t + get(y)) * dm(y);
    t += get(y, 0);
    return res;
  });
  if (ans >= h[0])
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
