#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    int n;
    cin>>n;
    ll h[n+1],d[n+1],a[n+1],s[n+1];
    ll ans=0;
    rep(i,n+1){
        cin>>h[i]>>a[i]>>d[i]>>s[i];
        if(i>0){
            a[i]-=d[0];
            d[i]=a[0]-d[i];
            if(a[i]<0)a[i]=0;
            if(d[i]<=0){
                if(a[i]>0){
                    cout<<-1<<endl;
                    return 0;
                }
                else d[i]=inf;
            }
            if(s[i]>s[0]){
                ans+=a[i];
            }
            h[i]=(h[i]+d[i]-1)/d[i];
        }
    }
    vector<int> ord(n);
    iota(ord.begin(),ord.end(),1);
    sort(ord.begin(),ord.end(),[&](int i,int j){
        return h[i]*a[j]<h[j]*a[i];
    });
    ll cur=0;
    for(auto i : ord){
        cur+=h[i];
        ans+=a[i]*(cur-1);
        if(ans>=h[0]){
            cout<<-1<<endl;
            return 0;
        }
    }
    if(ans<=h[0])cout<<ans<<endl;
    else cout<<-1<<endl;   
    return 0;
}
