#include <bits/stdc++.h>
using namespace std;
#define reep(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) reep((i),0,(n))
typedef pair<int,int> pii;
typedef pair<double,pii> P;
typedef long long ll;
#define mkp make_pair
#define F first
#define S second
#define int long long
// struct P {
// 	double first;
// 	pii second;
// 	P(double f, pii s) {
// 		first = f;
// 		second = s;
// 	}

// 	bool operator<(const P & obj) const {
// 		return this->S.S * obj.S.F > this->S.F * obj.S.S;
// 	}
// };

signed main(){
	int n;
	cin>>n;
	long long ans=0;
	n++;
	vector<int> h(n),a(n),d(n),s(n);
	rep(i,n) cin>>h[i]>>a[i]>>d[i]>>s[i];
	reep(i,1,n){
		a[i]=max(a[i]-d[0],0);
	}
	vector<int> atack(n);
	reep(i,1,n){
		atack[i]=max(a[0]-d[i],0);
	}
	vector<P> v;
	reep(i,1,n){
		if(a[i]==0){
			continue;
		}
		if(s[i]>s[0]) ans+=a[i];
		if(atack[i]==0){
			puts("-1");
			return 0;
		}
		int tmp=h[i]/atack[i];
		// cout<<h[i]<<" "<<atack[i]<<endl;
		if(h[i]%atack[i]) tmp++;
		// cout<<tmp<<endl;
		// v.push_back(mkp( (double)a[i]/tmp, pii(a[i],tmp)) );
		v.push_back(mkp( (double)a[i]/tmp, pii(a[i],tmp)) );
	}
	sort(v.begin(),v.end());
	reverse(v.begin(),v.end());
	ll turn=0;
	// rep(i,v.size()){
		// cout<<v[i].F<<" "<<v[i].S.F<<" "<<v[i].S.S<<endl;
	// }
	// ll ans1=LLONG_MAX;
	// do{
	// 	ll tmp=0;
	// 	turn=0;
	// 	rep(i,v.size()){
	// 		turn+=v[i].S.S;
	// 		tmp+=(turn-1)*v[i].S.F;
	// 	}
	// 	ans1=min(ans1,tmp+ans);
	// }while(next_permutation(v.begin(),v.end()));
	rep(i,v.size()){
		turn+=v[i].S.S;
		ans+=(turn-1)*v[i].S.F;
	}
	// if(ans1!=ans){
		// cout<<"NO"<<endl;
	// }
	if(ans>=h[0]) puts("-1");
	else cout<<ans<<endl;
}