#include <bits/stdc++.h>
using namespace std;
struct po {
  long long h, a, d, s, i;
};
long long n;
po A[40000];
bool compare(po a, po b) { return a.s > b.s; }
long long hp, a, d;
long long solve() {
  long long sum = 0, I = -1;
  priority_queue<pair<long long, long long> > Q;
  for (int i = 0; i <= n; i++) {
    Q.push(pair<long long, long long>(A[i].a, i));
    if (A[i].i == 0) {
      I = i;
      continue;
    }
    if (I == -1) hp -= max(A[i].a - d, 0LL);
    sum += max(A[i].a - d, 0LL);
  }
  while (!Q.empty()) {
    int idx = Q.top().second;
    Q.pop();
    po t = A[idx];
    if (idx == I) continue;
    if (hp < 0 || a - t.d <= 0) return -1;
    long long cost = ((A[idx].h - 1) / (a - t.d)) * sum;
    sum -= (A[idx].a - d);
    hp -= (cost + sum);
  }
  if (hp < 0) return -1;
  return A[I].h - hp;
}
int main() {
  cin >> n;
  for (int i = 0; i <= n; i++) {
    po &t = A[i];
    cin >> t.h >> t.a >> t.d >> t.s;
    t.i = i;
  }
  hp = A[0].h, a = A[0].a, d = A[0].d;
  sort(A, A + n + 1, compare);
  cout << solve() << endl;
  return 0;
}
