#include <bits/stdc++.h>
using namespace std;
int b[41000];
int c[41000];
int d[41000];
int e[41000];
struct wolf {
  int p, q;
  wolf() {}
  wolf(int A, int B) {
    p = A;
    q = B;
  }
};
inline bool operator<(const wolf& a, const wolf& b) {
  return a.p * b.q < a.q * b.p;
}
wolf t[41000];
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a + 1; i++) scanf("%d%d%d%d", b + i, c + i, d + i, e + i);
  long long ret = 0;
  long long now = 0;
  for (int i = 0; i < a; i++) {
    int p = max(c[i + 1] - d[0], 0);
    int r = max(c[0] - d[i + 1], 0);
    if (r == 0) {
      printf("-1\n");
      return 0;
    }
    int q = (b[i + 1] + r - 1) / r;
    if (e[i + 1] > e[0]) ret += p;
    t[i] = wolf(q, p);
    now += p;
  }
  std::sort(t, t + a);
  ret -= now;
  for (int i = 0; i < a; i++) {
    ret += (long long)t[i].p * now;
    now -= t[i].q;
  }
  if (ret < b[0])
    printf("%lld\n", ret);
  else
    printf("-1\n");
}
