//AOJ 2236
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef ll Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
//#define mt make_tuple
//typedef tuple<double,int,double> tp;
//typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const double EPS=1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int main(){
	int n;
	cin>>n;
	vp in;
	int a1,b1,c1,d1;
	cin>>a1>>b1>>c1>>d1;
	bool h=false;
	ll out=0;
	rep(i,n){
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		if(d1>d)out-=max(0,b-c1);
		if(b1-c>0)in.pb(pii(max(0,b-c1),(a+b1-c-1)/(b1-c)));
		else if(b-c1<=0);
		else h=true;
	}
	if(h){
		cout<<-1<<endl;
		return 0;
	}
	sort(all(in),[](pii a,pii b){
		return a.second*b.first<a.first*b.second;
	});
	ll co=0;
//	rep(i,in.size())cout<<in[i].first<<" "<<in[i].second<<endl;
	rep(i,in.size()){
		co+=in[i].second;
		if(out!=-inf)out+=co*in[i].first;
//		cout<<out<<endl;
		if(out>=a1)out=-inf;
		if(out==-inf)break;
	}
	if(out==-inf)cout<<-1<<endl;
	else cout<<out<<endl;
}