#include <bits/stdc++.h>
#define int long long
using namespace std;

const int INF = 1e9;

inline int divceil(int x,int y){
	return (y > 0 ? (x + y - 1) / y : INF);
}

signed main(){
	int n,h[40010],a[40010],d[40010],s[40010];
	cin >> n;
	for(int i = 0;i <= n;i++){
		cin >> h[i] >> a[i] >> d[i] >> s[i];
	}
	int ans = 0,sum = 0;
	for(int i = 1;i <= n;i++){
		if(s[0] > s[i]) ans -= max(0ll,a[i] - d[0]);
		sum += max(0ll,a[i] - d[0]);
	}
	vector<int> vec(n);
	iota(vec.begin(),vec.end(),1);
	sort(vec.begin(),vec.end(),[&](int i,int j){
		return divceil(h[i],a[0] - d[i]) * max(0ll,a[j] - d[0])
		< divceil(h[j],a[0] - d[j]) * max(0ll,a[i] - d[0]);
	});
	for(int v : vec){
		ans += sum * divceil(h[v],a[0] - d[v]);
		if(ans >= h[0]){
			cout << -1 << endl;
			return 0;
		}
		sum -= max(0ll,a[v] - d[0]);
	}
	cout << ans << endl;
}
