#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;

typedef tuple<double,int,int> T;// ??????/????????????????????°, ??????, ???????????°

int main(){
    int N;
    cin>>N;
    vector<int64_t> H(N+1), A(N+1), D(N+1), S(N+1);
    for(int i=0;i<=N;i++){
        cin>>H[i]>>A[i]>>D[i]>>S[i];
    }
    int64_t damage = 0, firepower = 0;
    vector<T> V;
    for(int i=1;i<=N;i++){
        if(S[i]>S[0])damage+=max(A[i]-D[0],0LL);
        int64_t fire=max(A[i]-D[0],0LL);
        firepower+=fire;
        if(max(A[0]-D[i],0LL)==0){
            cout<<-1<<endl;
            return 0;
        }
        int64_t turns=ceil(H[i]/(max(A[0]-D[i],0LL)));
        V.emplace_back(double(fire)/turns,fire,turns);
    }
    sort(V.begin(), V.end(),greater<T>());
    for(auto t:V){
        int64_t fire,turns;
        tie(ignore,fire,turns)=t;
        // cout<<fire<<' '<<turns<<endl;
        damage+=firepower*(turns-1);
        firepower-=fire;
        damage+=firepower;
    }
    if(H[0]>damage){
        cout<<damage<<endl;
    }else{
        cout<<-1<<endl;
    }
}