#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;
typedef long long ll;
struct chara{
    ll h, a, d, s, t;
    bool operator < (const chara& c) const {
        return t * c.a < c.t * a;
    }
};
ll solve(int n){
    chara me;
    cin >> me.h >> me.a >> me.d >> me.s;

    vector<chara> rival(n);
    for(chara& c : rival){
        cin >> c.h >> c.a >> c.d >> c.s;
        c.a = max(0ll, c.a - me.d);
    }

    for(chara& c : rival){
        if(me.a <= c.d){
            if(c.a > 0){
                return -1;
            }else{
                c.d = 0;
            }
        }
    }

    for(chara& c : rival){
        c.t = (c.h + me.a - c.d - 1) / (me.a - c.d);
    }

    ll sum = 0;
    for(const chara& c : rival){
        if(c.s < me.s){
            sum -= c.a;
        }
    }

    sort(rival.begin(), rival.end());

    ll time = 0;
    for(const chara& c : rival){
        time += c.t;
        sum += c.a * time;
        if(sum >= me.h) {
            return -1;
        }
    }

    return sum;
}

int main(){
    int n;
    while(cin >> n){
        cout << solve(n) << endl;
    }
    return 0;
}