#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> h(n + 1), a(n + 1), d(n + 1), s(n + 1);
  for (int i = (0); i < (int)(n + 1); ++i) cin >> h[i] >> a[i] >> d[i] >> s[i];
  long long ans = 0;
  long long sum = 0;
  vector<pair<int, int>> e;
  for (int i = (1); i < (int)(n + 1); ++i) {
    int dmg = a[i] - d[0];
    if (dmg <= 0) continue;
    if (s[i] > s[0]) ans += dmg;
    if (d[i] >= a[0]) {
      cout << -1 << endl;
      return 0;
    }
    int x = a[0] - d[i];
    int turn = (h[i] + x - 1) / x;
    e.push_back(make_pair(dmg, turn));
    sum += dmg;
  }
  if (sum >= 2e9) {
    cout << -1 << endl;
    return 0;
  }
  sort(e.begin(), e.end(),
       [](const pair<int, int> &lhs, const pair<int, int> &rhs) {
         return 1. * lhs.second / lhs.first < 1. * rhs.second / rhs.first;
       });
  for (auto p : e) {
    ans += sum * p.second - p.first;
    if (ans > h[0]) {
      ans = -1;
      break;
    }
    sum -= p.first;
  }
  cout << ans << endl;
}
