#include <bits/stdc++.h>
using namespace std;
typedef tuple<double, int64_t, int64_t> T;
int main() {
  int N;
  cin >> N;
  vector<int64_t> H(N + 1), A(N + 1), D(N + 1), S(N + 1);
  for (int i = 0; i <= N; i++) {
    cin >> H[i] >> A[i] >> D[i] >> S[i];
  }
  int64_t damage = 0, firepower = 0;
  vector<T> V;
  for (int i = 1; i <= N; i++) {
    int64_t fire = max(A[i] - D[0], int64_t(0));
    if (S[i] > S[0]) damage += fire;
    firepower += fire;
    if (max(A[0] - D[i], int64_t(0)) == 0) {
      cout << -1 << endl;
      return 0;
    }
    int64_t turns = ceil(double(H[i]) / (max(A[0] - D[i], int64_t(0))));
    V.emplace_back(double(fire) / turns, fire, turns);
  }
  sort(V.begin(), V.end(), greater<T>());
  for (auto t : V) {
    int64_t fire, turns;
    tie(ignore, fire, turns) = t;
    damage += firepower * (turns - 1);
    firepower -= fire;
    damage += firepower;
    if (damage >= H[0]) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << damage << endl;
}
