#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <functional>
#include <iostream>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n;

ll ma,md,ms,mh;

struct data{
	ll h,a,d,s;
	data(){}
	data(ll hh,ll aa,ll dd,ll ss){
		h=hh;
		a=aa;
		d=dd;
		s=ss;
	}
	bool operator <(const data& da)const{
		ll turn=(h+(ma-d-1LL))/(ma-d);
		ll dturn=(da.h+(ma-da.d-1LL))/(ma-da.d);
		ll sum=turn*max(0LL,a-md)+(turn+dturn)*max(0LL,da.a-md);
		ll dsum=(turn+dturn)*max(0LL,a-md)+dturn*max(0LL,da.a-md);
		return sum<dsum;
	}
};


vector<data> vec;

int main(void){
	scanf("%d",&n);
	ll sum=0;
	scanf("%lld%lld%lld%lld",&mh,&ma,&md,&ms);
	for(int i=0;i<n;i++){
		ll h,a,d,s;
		scanf("%lld%lld%lld%lld",&h,&a,&d,&s);
		if(ma-d<=0LL){
			if(a-md>0){
				printf("-1\n");
				return 0;
			}
		}else{
			sum+=max(0LL,a-md);
			vec.push_back(data(h,a,d,s));
		}
	}
	sort(vec.begin(),vec.end());
	ll res=0;
	for(int i=0;i<vec.size();i++){
		ll turn=(vec[i].h+(ma-vec[i].d-1LL))/(ma-vec[i].d);
		res+=turn*sum;
		if(vec[i].s<ms)res-=max(0LL,vec[i].a-md);
		if(res>=mh){
			printf("-1\n");
			return 0;
		}
		sum-=max(0LL,vec[i].a-md);
	}
	printf("%lld\n",res);
	return 0;
}