#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstring>
#include <functional>
#include <cmath>
#include <complex>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<int,P> PP;
ll dam[40000],t[40000],inf=1e9;
vector<PP> ord;
bool comp(const PP& x,const PP& y){
	return x.sc.fs*y.sc.sc>x.sc.sc*y.sc.fs;
}
vector<int> uses;
int main(){
	int N;
	ll H,A,D,S,turn=0,sum=0;
	cin>>N;
	cin>>H>>A>>D>>S;
	rep(i,N){
		ll h,a,d,s;
		cin>>h>>a>>d>>s;
		dam[i]=max((ll)0,a-D);
		if(A-d>0){
			t[i]=(h-1)/(A-d)+1;
			turn+=t[i];
		} else t[i]=-1;
		if(S<s) sum+=dam[i];
	}
	rep(i,N){
		if(!dam[i]) continue;
		if(t[i]<0){
			puts("-1");
			return 0;
		}
		uses.pb(i);
	}
	for(int i:uses) ord.pb(PP(i,P(dam[i],t[i])));
	sort(all(ord),comp);
	rep(i,ord.size()) dam[i]=ord[i].sc.fs,t[i]=ord[i].sc.sc;
	ll tmp=-1;
	rep(i,ord.size()){
//		show(t[i]);
//		show(dam[i]);
		tmp+=t[i];
		sum+=dam[i]*tmp;
		if(H-sum<=0){
			puts("-1");
			return 0;
		}
	}
	cout<<sum<<endl;
}