#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;


int main(){
    int n;
    cin >> n;
    vector<long long>h(n),a(n),d(n),s(n);
    long long h1,a1,d1,s1;
    long long ans=0;
    cin >> h1 >> a1 >> d1 >> s1;
    for(int i=0;i<n;i++){
        cin >> h[i] >> a[i] >> d[i] >> s[i];
        if(s1<s[i]){
            ans += max(a[i]-d1,0LL);
        }
    }
    if(h1<=ans){
        cout << -1 << endl;
        return 0;
    }
    vector<long long> c(n),dam(n);
    for(int i=0;i<n;i++){
        if(max(a1-d[i],0LL)!=0){
            c[i] = (h[i]-1)/(a1-d[i]) + 1;
        }else{
            c[i] = 0;
            if(max(a[i]-d1,0LL)!=0){
                cout << -1 << endl;
                return 0;
            }
        }
        dam[i] = max(a[i]-d1,0LL);
    }
    vector<pair<long double,pair<long long,long long > > > v;
    long long sm = 0;
    for(int i=0;i<n;i++){
        if(c[i]!=0){
            v.push_back(make_pair((long double)dam[i]/(long double)c[i],make_pair(c[i],dam[i])));
            sm += dam[i];
        }
    }
    sort(v.begin(),v.end(),greater<pair<long double,pair<long long,long long > > >());
    for(int i=0;i<v.size();i++){
        ans += sm*(v[i].second.first-1);
        sm -= v[i].second.second;
        ans += sm;
        if(h1<=ans){
            cout << -1 << endl;
            return 0;
        }
    }
    if(h1<=ans){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}
