#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
long solve() {
  int n;
  cin >> n;
  long ret = 0;
  vector<long> h(n + 1), a(n + 1), d(n + 1), s(n + 1), t(n + 1);
  for (int i = (int)(0); i < (int)(n + 1); i++)
    cin >> h[i] >> a[i] >> d[i] >> s[i];
  for (int i = (int)(1); i < (int)(n + 1); i++) {
    if (a[0] <= d[i]) return -1;
    long tmp = a[0] - d[i];
    t[i] = (h[i] + tmp - 1) / tmp;
  }
  for (int i = (int)(1); i < (int)(n + 1); i++)
    if (s[0] > s[i]) {
      h[0] += max(a[i] - d[0], 0L);
      ret -= max(a[i] - d[0], 0L);
    }
  vector<pair<double, int>> vec(n);
  for (int i = (int)(1); i < (int)(n + 1); i++) {
    double tmp = max(a[i] - d[0], 0L);
    double key;
    if (tmp == 0) key = -1;
    key = (double)t[i] / tmp;
    vec[i - 1] = make_pair((key), (i));
  }
  sort((vec).begin(), (vec).end());
  long acctime = 0;
  for (int i = (int)(0); i < (int)(n); i++) {
    int id = vec[i].second;
    acctime += t[id];
    h[0] -= max(a[id] - d[0], 0L) * acctime;
    ret += max(a[id] - d[0], 0L) * acctime;
  }
  if (h[0] <= 0)
    return -1;
  else
    return ret;
}
int main() {
  cout << solve() << endl;
  return 0;
}
