#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

typedef pair<ll,ll> pll;

bool cmp(const pll &a, const pll &b) {
  return (ll)b.first*a.second < (ll)a.first*b.second;
}
int main() {
  ll n;
  cin >> n;
  vector<pll> v;
  ll h0,a0,d0,s0;
  cin >> h0 >> a0 >> d0 >> s0;
  ll hp = h0;
  ll sum = 0;
  REP(i,n) {
    ll h,a,d,s;
    cin >> h >> a >> d >> s;
    ll damage = max(a-d0, 0LL);
    ll attack = max(a0-d, 0LL);
    if (attack == 0) {
      if (damage) {
        cout << -1 << endl;
        return 0;
      }
      continue;
    }
    ll turn = (h + attack - 1) / attack;
    if (s0 < s) hp -= damage;
    v.push_back(pll(damage, turn));
    sum += damage;
  }
  sort(ALL(v), cmp);
  REP(i,v.size()) {
    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;
}