#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  return (long long)b.first * a.second < (long long)a.first * b.second;
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int> > v;
  int h0, a0, d0, s0;
  cin >> h0 >> a0 >> d0 >> s0;
  long long hp = h0;
  long long sum = 0;
  for (int i = 0; i < (int)n; ++i) {
    int h, a, d, s;
    cin >> h >> a >> d >> s;
    int damage = max(a - d0, 0);
    int attack = max(a0 - d, 0);
    if (attack == 0) {
      cout << -1 << endl;
      return 0;
    }
    int turn = (h + attack - 1) / attack;
    if (s0 < s) hp -= damage;
    v.push_back(pair<int, int>(damage, turn));
    sum += damage;
  }
  sort((v).begin(), (v).end(), cmp);
  for (int i = 0; i < (int)n; ++i) {
    if (hp < 0) break;
    sum -= v[i].first;
    if (sum > hp / v[i].second) {
      hp = -1;
      break;
    }
    hp -= sum * v[i].second;
    hp -= v[i].first * (v[i].second - 1);
  }
  if (hp < 0)
    cout << -1 << endl;
  else
    cout << h0 - hp << endl;
}
