#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B>
bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
long long inf = sizeof(long long) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  int n;
  cin >> n;
  vector<pair<long long, long long> > in(n);
  int a1, b1, c1, d1;
  cin >> a1 >> b1 >> c1 >> d1;
  bool h = false;
  long long out = 0;
  for (int i = 0; i < n; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    b = max(0, b - c1);
    if (d1 > d) out -= b;
    if (b1 - c > 0)
      in[i] = pair<long long, long long>(b, (a + b1 - c - 1) / (b1 - c));
    else
      h = true;
  }
  if (h) {
    cout << -1 << endl;
    return 0;
  }
  sort(in.begin(), in.end(),
       [](pair<long long, long long> a, pair<long long, long long> b) {
         return a.second * b.first < a.first * b.second;
       });
  long long co = 0;
  for (int i = 0; i < n; i++) {
    co += in[i].second;
    out += co * in[i].first;
  }
  if (out >= a1)
    cout << -1 << endl;
  else
    cout << out << endl;
}
