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

typedef long long ll;

int N;
vector<int> H, A, D, S;

typedef struct{
    int turn, ad;
}Enemy;

bool operator < (const Enemy& l, const Enemy& r){
    return l.turn * r.ad > r.turn * l.ad;
}

int solve(){
    ll damage = 0;
    vector<bool> ignore(N, false);
    
    for(int i=1; i<=N; i++){
        if(S[i] > S[0])
            damage += max(0, A[i]-D[0]);
        if(D[i] >= A[0]){
            if(A[i] > D[0])
               return -1;
            else
                ignore[i] = true;
        }
    }
    
    vector<Enemy> enemies;
    for(int i=1; i<=N; i++) if(!ignore[i]){
        int turn = (H[i]+A[0]-D[i]-1) / (A[0]-D[i]);
        Enemy enemy({turn, max(0, A[i]-D[0])});
        enemies.push_back(enemy);
    }
    sort(enemies.begin(), enemies.end());
    
    ll sd = 0;
    for(int i=0; i<enemies.size(); i++){
        sd += enemies[i].ad;
        damage += sd * enemies[i].turn;
        damage -= enemies[i].ad;
        if(damage >= H[0])
            return -1;
    }
    return damage;
}

int main(){
    cin >> N;
    
    H.resize(N+1); A.resize(N+1); D.resize(N+1), S.resize(N+1);
    for(int i=0; i<=N; i++)
        cin >> H[i] >> A[i] >> D[i] >> S[i];
    
    cout << solve() << endl;
    
    return 0;
}