#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  ll h[n + 1], d[n + 1], a[n + 1], s[n + 1];
  ll ans = 0;
  for (int i = (int)(0); i < (int)(n + 1); ++i) {
    cin >> h[i] >> a[i] >> d[i] >> s[i];
    if (i > 0) {
      a[i] -= d[0];
      d[i] = a[0] - d[i];
      if (a[i] < 0) a[i] = 0;
      if (d[i] <= 0) {
        if (a[i] > 0) {
          cout << -1 << endl;
          return 0;
        } else
          d[i] = inf;
      }
      if (s[i] > s[0]) {
        ans += a[i];
      }
      h[i] = (h[i] + d[i] - 1) / d[i];
    }
  }
  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 1);
  sort(ord.begin(), ord.end(),
       [&](int i, int j) { return h[i] * a[j] < h[j] * a[i]; });
  ll cur = 0;
  for (auto i : ord) {
    cur += h[i];
    ans += a[i] * (cur - 1);
  }
  if (ans < h[0])
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
