#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long ans = 0;
  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];
  for (int i = (1); i < (n); i++) {
    a[i] = max(a[i] - d[0], 0);
  }
  vector<int> atack(n);
  for (int i = (1); i < (n); i++) {
    atack[i] = max(0, a[0] - d[i]);
  }
  vector<pair<double, pair<int, int> > > v;
  for (int i = (1); i < (n); i++) {
    if (a[i] == 0) {
      continue;
    }
    if (s[i] > s[0]) ans += a[i];
    if (atack[i] == 0) {
      puts("-1");
      return 0;
    }
    int tmp = h[i] / atack[i];
    if (h[i] % atack[i]) tmp++;
    v.push_back(make_pair((double)a[i] / tmp, pair<int, int>(a[i], tmp)));
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  long long turn = 0;
  for (int(i) = (0); (i) < ((v.size())); (i)++) {
    turn += v[i].second.second;
    ans += (turn - 1) * v[i].second.first;
  }
  if (ans >= h[0])
    puts("-1");
  else
    cout << ans << endl;
}
