#include <bits/stdc++.h>
using namespace std;
struct Data {
  long long T, D;
  Data() {}
  Data(long long a, long long b) { T = a, D = b; }
  bool operator<(const Data& obj) const {
    if (obj.D == 0) return D != 0;
    if (D == 0) return false;
    return (double)T / D < (double)obj.T / obj.D;
  }
};
long long n;
long long h[40005], a[40005], d[40005], s[40005];
vector<Data> vec;
int main(void) {
  cin >> n;
  for (int i = 0; i <= n; i++) cin >> h[i] >> a[i] >> d[i] >> s[i];
  bool pWin = true, mWin = false;
  for (int i = 1; i <= n; i++) {
    if (a[0] - d[i] <= 0) {
      pWin = false;
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] - d[0] > 0) {
      mWin = true;
      break;
    }
  }
  if (!pWin) {
    if (mWin)
      cout << -1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }
  if (!mWin) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    vec.push_back(
        Data((h[i] + a[0] - d[i] - 1) / (a[0] - d[i]), max(0LL, a[i] - d[0])));
  }
  sort(vec.begin(), vec.end());
  long long ans = 0, sumT = 0;
  for (int i = 0; i < vec.size(); i++) {
    sumT += vec[i].T;
    ans += sumT * vec[i].D;
  }
  for (int i = 1; i <= n; i++) {
    if (s[0] > s[i]) ans -= max(0LL, a[i] - d[0]);
  }
  if (ans >= h[0]) ans = -1;
  cout << ans << endl;
  return 0;
}
