#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 A[40404];
long long B[40404];
long long ceil(long long A, long long B) { return A / B + min(1LL, A % B); }
int main() {
  cin >> n;
  vector<int> vec;
  for (int i = (0); i <= (n); i++) {
    cin >> h[i] >> a[i] >> d[i] >> s[i];
    if (i == 0) continue;
    if (a[0] <= d[i] && a[i] > d[0]) {
      cout << -1 << endl;
      return 0;
    }
    if (a[0] > d[i]) {
      vec.push_back(i);
      A[i] = (ceil(h[i], a[0] - d[i]));
      B[i] = (max(0LL, a[i] - d[0]));
    }
  }
  auto ope = [&](int i, int j) { return A[i] * B[j] < A[j] * B[i]; };
  sort(vec.begin(), vec.end(), ope);
  long long HP = 0;
  long long cou = 0;
  for (int i = (0); i <= (vec.size() - 1); i++) {
    int index = vec[i];
    cou += A[index];
    HP += cou * B[index];
    if (s[index] < s[0]) HP -= B[index];
    if (h[0] <= HP) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << HP << endl;
  return 0;
}
