#include <iostream>
#include <vector>
#include <algorithm>
#define llint long long

using namespace std;

struct Data{
	llint T, D;
	Data(){}
	Data(llint a, llint b){
		T = a, D = b;
	}
	bool operator<(const Data& obj)const{
		if(obj.D == 0) return D != 0;
		if(D == 0) return false;
		return (double)T/D < (double)obj.T/obj.D;
	}
};

llint n;
llint h[40005], a[40005], d[40005], s[40005];
vector<Data> vec;

int main(void)
{
	cin >> n;
	for(int i = 0; i <= n; i++) cin >> h[i] >> a[i] >> d[i] >> s[i];
	
	bool pWin = true, mWin = false;
	for(int i = 1; i <= n; i++){
		if(a[0] - d[i] <= 0 && a[i] - d[0] > 0){
			pWin = false;
			break;
		}
	}
		
	if(!pWin){
		cout << -1 << endl;
		return 0;
	}
	
	for(int i = 1; i <= n; i++){
		if(a[0] - d[i] <= 0) continue;
		vec.push_back(Data( (h[i]+a[0]-d[i]-1)/(a[0]-d[i]), max(0LL, a[i] - d[0]) ) );
	}
	sort(vec.begin(), vec.end());
	
	llint ans = 0, sumT = 0;
	for(int i = 1; i <= n; i++){
		if(s[0] > s[i]) ans -= max(0LL, a[i] - d[0]);
	}
	for(int i = 0; i < vec.size(); i++){
		sumT += vec[i].T;
		ans += sumT * vec[i].D;
		if(ans >= h[0]){
			cout << -1 << endl;
			return 0;
		}
	}
	if(ans >= h[0]) ans = -1;
	cout << ans << endl;
	
	return 0;
}

