#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> H, A, D, S;
typedef struct {
  int turn, ad;
} Enemy;
bool operator<(const Enemy& l, const Enemy& r) {
  return l.turn * r.ad > r.turn * l.ad;
}
int solve() {
  long long damage = 0;
  for (int i = 1; i <= N; i++) {
    if (S[i] > S[0]) damage += max(0, A[i] - D[0]);
    if (D[i] >= A[0]) return -1;
  }
  vector<Enemy> enemies;
  for (int i = 1; i <= N; i++) {
    int turn = (H[i] + A[0] - D[i] - 1) / (A[0] - D[i]);
    Enemy enemy({turn, A[i] - D[0]});
    enemies.push_back(enemy);
  }
  sort(enemies.begin(), enemies.end());
  long long sd = 0;
  for (int i = 0; i < N; i++) {
    sd += enemies[i].ad;
    damage += sd * enemies[i].turn;
    damage -= enemies[i].ad;
    if (damage >= H[0]) return -1;
  }
  return damage;
}
int main() {
  cin >> N;
  H.resize(N + 1);
  A.resize(N + 1);
  D.resize(N + 1), S.resize(N + 1);
  for (int i = 0; i <= N; i++) cin >> H[i] >> A[i] >> D[i] >> S[i];
  cout << solve() << endl;
  return 0;
}
