#include<stdio.h>
#include<algorithm>
using namespace std;
int b[41000];
int c[41000];
int d[41000];
int e[41000];
struct wolf{
	int p,q;
	wolf(){}
	wolf(int A,int B){
		p=A;q=B;
	}
};
inline bool operator<(const wolf&a,const wolf&b){
	return (long long)a.p*b.q<(long long)a.q*b.p;
}
wolf t[41000];
int main(){
	int a;scanf("%d",&a);
	for(int i=0;i<a+1;i++)scanf("%d%d%d%d",b+i,c+i,d+i,e+i);
	long long ret=0;
	long long now=0;
	int n=0;
	for(int i=0;i<a;i++){
		int p=max(c[i+1]-d[0],0);
		int r=max(c[0]-d[i+1],0);
		if(r==0&&p!=0){
			printf("-1\n");return 0;
		}else if(r==0){
			continue;
		}
		int q=(b[i+1]+r-1)/r;
		if(e[i+1]>e[0])ret+=p;
		t[n++]=wolf(q,p);
		now+=p;
	//	printf("%d %d\n",p,q);
	}
	std::sort(t,t+n);
	ret-=now;
	for(int i=0;i<n;i++){
		ret+=(long long)t[i].p*now;
		now-=t[i].q;
		if(ret>=b[0])break;
	}
	if(ret<b[0])printf("%lld\n",ret);
	else printf("-1\n");
}