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

using ll=long long;
int main(){
    int n;
    cin>>n;
    ll ph,pa,pd,ps;
    vector<ll> h(n),a(n),d(n),s(n);
    cin>>ph>>pa>>pd>>ps;
    for(int i=0;i<n;i++){
        cin>>h[i]>>a[i]>>d[i]>>s[i];
    }
    vector<ll> t(n);
    bool poyo=false;
    for(int i=0;i<n;i++){
        if(d[i]>=pa){
            if(a[i]-pd<=0){
                poyo=true;
                t[i]=1;
            }
            else{
                cout<<-1<<endl;
                return 0;
            }
        }
        else{
            t[i]=h[i]/(pa-d[i])+(int)(h[i]%(pa-d[i])>0);
        }
    }
    vector<int> idx(n);
    iota(idx.begin(),idx.end(),0);
    auto cmp=[&](int lhs,int rhs){return (a[lhs]-pd)*t[rhs]>(a[rhs]-pd)*t[lhs];};
    sort(idx.begin(),idx.end(),cmp);
    ll dmg=0;
    for(int i=0;i<n;i++){
        if(s[i]>ps){
            dmg+=max(a[i]-pd,0LL);
        }
    }
    ll sumt=0;
    for(int i=0;i<n;i++){
        int id=idx[i];
        sumt+=t[id];
        if(sumt-1>=1e9+1 && a[id]-pd>0){
            cout<<-1<<endl;
            return 0;
        }
        dmg+=max(a[id]-pd,0LL)*(sumt-1);
        if(dmg>=ph){
            cout<<-1<<endl;
            return 0;
        }
    }
    if(dmg<ph){
        cout<<dmg<<endl;
    }
    else{
        cout<<-1<<endl;
    }
    return 0;
}
