#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int lli;

struct info{
    int h,a;
    double rate;
    info(int h, int a, double r):h(h), a(a), rate(r){}
    info(){}
};
bool comp(const info& a, const info& b){
    return a.rate > b.rate;
}

int main(){
    int n;
    cin >> n;

    lli h,a,d,s;
    cin >> h >> a >> d >> s;
    vector<info> e(n);
    lli sum = 0, damage = 0;
    for(int i=0; i<n; i++){
        int eh,ea,ed,es;
        cin >> eh >> ea >> ed >> es;
        ea -= d;
        if(ea <= 0){
            e[i] = info(1, 0, 0);
            continue;
        }
        if(es > s){
            damage += ea;
        }
        if(a-ed <= 0){
            h = 0;
            continue;
        }
        eh = (eh+(a-ed)-1)/(a-ed);
        e[i] = info(eh, ea, (double)ea/eh);
        sum += ea;
    }
    if(h==0){
        cout << -1 << endl;
        return 0;
    }

    sort(e.begin(), e.end(), comp);
    for(int i=0; sum>0; i++){
        int limit = (h-damage)/sum;
        if(e[i].h > limit){
            h=0;
            break;
        }
        damage += sum*(e[i].h-1);
        sum -= e[i].a;
        damage += sum;
    }
    if(h <= damage){
        cout << -1 << endl;
    }else{
        cout << damage << endl;
    }
    return 0;
}