#include <bits/stdc++.h>
using namespace std;
int n;
long long h[40404];
long long a[40404];
long long d[40404];
long long s[40404];
long long ceil(long long A, long long B) { return A / B + min(1LL, A % B); }
int main() {
  cin >> n;
  priority_queue<pair<long long, long long> > que;
  long long sum = 0;
  for (int i = (0); i <= (n); i++) {
    cin >> h[i] >> a[i] >> d[i] >> s[i];
    if (i == 0) continue;
    que.push({max(0LL, a[i] - d[0]), i});
    sum += max(0LL, a[i] - d[0]);
  }
  long long HP = 0;
  while (!que.empty()) {
    pair<long long, long long> p = que.top();
    long long i = que.top().second;
    que.pop();
    long long damage = max(0LL, a[0] - d[i]);
    if (damage == 0 && p.first == 0) {
      continue;
    }
    if (damage == 0 && p.first > 0) {
      cout << -1 << endl;
      return 0;
    }
    long long turn = ceil(h[i], damage);
    HP += turn * sum;
    if (s[0] > s[i]) {
      HP -= p.first;
    }
    sum -= p.first;
  }
  if (h[0] > HP) {
    cout << HP << endl;
  } else {
    cout << -1 << endl;
  }
}
