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

using ll=long long;
int main(){
    int n;
    cin>>n;
    ll hh,aa,dd,ss;
    cin>>hh>>aa>>dd>>ss;
    vector<ll> h(n),a(n),d(n),s(n);
    for(int i=0;i<n;i++){
        cin>>h[i]>>a[i]>>d[i]>>s[i];
    }
    ll res=0;
    for(int i=0;i<n;i++){
        if(ss<s[i]){
            res+=max(a[i]-dd,0);
        }
        if(aa<=d[i] || res>=hh){
            cout<<-1<<endl;
            return 0;
        }
    }
    vector<ll> ta(n),th(n);
    for(int i=0;i<n;i++){
        ta[i]=max(a[i]-dd,0);
        th[i]=(h[i]/(aa-d[i]))+(h[i]%(aa-d[i])!=0);
        if((th[i]-1)*ta[i]>=hh){
            cout<<-1<<endl;
            return 0;
        }
    }
    vector<tuple<int,ll,ll>> ene(n);
    for(int i=0;i<n;i++){
        ene[i]=make_tuple(i,ta[i],th[i]);
    }
    auto comp=[](const tuple<int,ll,ll> &lhs,const tuple<int,ll,ll> rhs){
        return get<2>(lhs)*get<1>(rhs)<get<1>(lhs)*get<2>(rhs);
    };
    sort(ene.begin(),ene.end(),comp);
    vector<int> p(n);
    for(int i=0;i<n;i++){
        p[i]=get<0>(ene[i]);
    }
    ll turn=0;
    for(int i=0;i<n;i++){
        turn+=th[p[i]];
        if(ta[p[i]]!=0 && turn-1>=hh){
            cout<<-1<<endl;
            return 0;
        }
        ll ret=(turn-1)*ta[p[i]];
        if(ret>=hh){
            cout<<-1<<endl;
            return 0;
        }
        res+=ret;
        if(res>=hh){
            cout<<-1<<endl;
            return 0;
        }
    }
    if(res>=hh){
        cout<<-1<<endl;
        return 0;
    }
    cout<<res<<endl;
    return 0;
}
