#include <bits/stdc++.h>
using namespace std;
struct enemy {
  long long time, attack;
};
bool sorter(const enemy& a, const enemy& b) {
  return a.time * b.attack < b.time * a.attack;
}
long long n;
long long h[40040], a[40040], d[40040], s[40040];
enemy es[40040];
int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  n++;
  for (long long i = (long long)(0); i < (long long)(n); i++)
    cin >> h[i] >> a[i] >> d[i] >> s[i];
  for (long long i = (long long)(1); i < (long long)(n); i++) {
    long long damege = (0 > a[0] - d[i] ? 0 : a[0] - d[i]);
    if (damege == 0) {
      cout << -1 << endl;
      return 0;
    }
    es[i].time = (h[i] + damege - 1) / damege;
    es[i].attack = (0 > a[i] - d[0] ? 0 : a[i] - d[0]);
  }
  long long res = 0, sum = 0;
  for (long long i = (long long)(1); i < (long long)(n); i++) {
    if (s[i] > s[0]) {
      res += es[i].attack;
    }
    sum += es[i].attack;
  }
  sort(es + 1, es + n, sorter);
  for (long long i = (long long)(1); i < (long long)(n); i++) {
    res += sum * (es[i].time - 1) + (sum - es[i].attack);
    if (res >= h[0]) {
      cout << -1 << endl;
      return 0;
    }
    sum -= es[i].attack;
  }
  if (res >= h[0])
    cout << -1 << endl;
  else
    cout << res << endl;
  return 0;
}
