#include <bits/stdc++.h>
using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  int n;
  cin >> n;
  LL h,a,d,s;
  cin >> h >> a >> d >> s;
  vector<LL> eh(n),ea(n),ed(n),es(n);
  vector<pair<LD,LL>> ss(n,{0,-1});
  LL ans=0;
  for(int i=0;i<n;i++){
    cin >> eh[i] >> ea[i] >> ed[i] >> es[i];
    ea[i]=max(ea[i]-d,0);
    ed[i]=max(a-ed[i],0);
    if(es[i]>s) ans+=ea[i];
    if(ed[i]==0){
      if(ea[i]>0){
        cout << -1 << endl;
        return 0;
      }
    }else{
      if(ea[i]==0) ss[i]={INF,i};
      else ss[i]={((eh[i]+ed[i]-1)/ed[i])/(LD)ea[i],i};
    }
  }
  sort(all(ss));
  LL turn=0;
  int id;
  // cout << ans << endl;
  for(int i=0;i<n;i++){
    if(ss[i].second!=-1){
      id=ss[i].second;
      turn+=(eh[id]+ed[id]-1)/ed[id];
      ans+=(turn-1)*ea[id];
      // cout << id SP turn SP ans << endl;
      if(ans>=h){
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

