#include <bits/stdc++.h>
using namespace std;
int n;
long long int h[44444];
long long int a[44444];
long long int b[44444];
long long int s[44444];
pair<long double, int> p[44444];
long long int ad;
int main(void) {
  cin >> n;
  for (int i = 0; i <= n; i++) {
    cin >> h[i] >> a[i] >> b[i] >> s[i];
  }
  for (int i = 1; i <= n; i++) {
    ad += max(a[i] - b[0], 0LL);
    if (a[0] - b[i] <= 0) {
      p[i - 1].first = -1;
    } else {
      p[i - 1].first =
          (long double)max(a[i] - b[0], 0LL) /
          (long long int)((h[i] + a[0] - b[i] - 1) / (a[0] - b[i]));
    }
    p[i - 1].second = i;
  }
  sort(p, p + n, greater<pair<long double, int> >());
  long long int d = 0;
  for (int i = 0; i < n; i++) {
    int pp = p[i].second;
    if (p[i].first < 0) {
      continue;
    }
    d += ad * ((h[pp] + a[0] - b[pp] - 1) / (a[0] - b[pp]));
    if (s[0] > s[pp]) d -= max(a[pp] - b[0], 0LL);
    if (h[0] <= d) {
      cout << -1 << endl;
      return 0;
    }
    ad -= max(a[pp] - b[0], 0LL);
  }
  cout << d << endl;
}
