#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& a) {
  os << '[';
  for (int i = int(0); i < int(a.size()); i++) os << (i ? " " : "") << a[i];
  return os << ']';
}
void permute(vector<int>& a, const vector<int>& is) {
  int n = a.size();
  for (int i : is) a.push_back(a[i]);
  a.erase(begin(a), begin(a) + n);
}
int solve(vector<int>& hs, vector<int>& as, vector<int>& ds, vector<int>& ss) {
  {
    vector<int> is(1, 0);
    for (int i = int(1); i < int(hs.size()); i++) {
      int a = max(as[0] - ds[i], 0);
      int b = max(as[i] - ds[0], 0);
      if (a == 0)
        if (b == 0)
          ;
        else
          return -1;
      else
        is.push_back(i);
    }
    permute(hs, is);
    permute(as, is);
    permute(ds, is);
    permute(ss, is);
  }
  int n = hs.size();
  vector<int> ts(n);
  vector<int> ps(n);
  for (int i = int(1); i < int(n); i++) {
    int dam = max(as[0] - ds[i], 0);
    assert(dam > 0);
    ts[i] = (hs[i] + dam - 1) / dam;
    ps[i] = max(as[i] - ds[0], 0);
  }
  long long dam = 0;
  for (int i = int(1); i < int(n); i++)
    if (ss[i] > ss[0]) dam += ps[i];
  {
    vector<int> is(n);
    iota(begin(is), end(is), 0);
    sort(begin(is), end(is), [&](int i, int j) { return ss[i] > ss[j]; });
    permute(ts, is);
    permute(ps, is);
    for (int i = int(0); i < int(n); i++)
      if (is[i] == 0) {
        rotate(begin(ts), begin(ts) + i + 1, end(ts));
        ts.pop_back();
        rotate(begin(ps), begin(ps) + i + 1, end(ps));
        ps.pop_back();
        n--;
        break;
      }
  }
  long long h = hs[0];
  {
    vector<int> is(n);
    iota(begin(is), end(is), 0);
    sort(begin(is), end(is), [&](int i, int j) {
      return long long(ps[i]) * (ts[i]) > long long(ps[j]) * (ts[j]);
    });
    permute(ts, is);
    permute(ps, is);
  }
  vector<long long> sum(n + 1);
  for (int i = int(n); i-- > int(0);) sum[i] = sum[i + 1] + ps[i];
  bool ok = true;
  for (int i = int(0); i < int(n); i++) {
    if (h <= dam + 1. * ts[i] * sum[i] - ps[i]) {
      ok = false;
      break;
    }
    dam += long long(ts[i]) * sum[i] - ps[i];
  }
  return ok ? dam : -1;
}
int main() {
  for (int n; cin >> n && n;) {
    vector<int> hs(n + 1), as(n + 1), ds(n + 1), ss(n + 1);
    for (int i = int(0); i < int(n + 1); i++)
      cin >> hs[i] >> as[i] >> ds[i] >> ss[i];
    cout << solve(hs, as, ds, ss) << endl;
  }
}
