#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int n;
ll a[40001], t[40001];
ll H, A, D, S;
typedef pair<ld, int> speP;
vector<speP> v;
int main() {
	cin >> n;
	ll ans = 0;
	cin >> H >> A >> D >> S;
	bool valid = true;
	rep(i, n) {
		ll h, d, s;
		cin >> h >> a[i] >> d >> s;
		a[i] = max(a[i] - D, (ll)0);
		if (s < S) {
			ans -= a[i];
		}
		ll damage = A - d;
		if (damage <= 0&&a[i]>0) {
			valid = false; break;
		}
		else if (damage <= 0)continue;
		else {
			t[i] = (h + damage - 1) / damage;
			v.push_back({ a[i] / (ld)t[i], i });
		}
	}
	if (!valid)cout << -1 << endl;
	else {
		sort(v.begin(), v.end(),greater<speP>());
		ll sum = 0;
		rep(i, v.size()) {
			int id = v[i].second;
			sum += t[id];
			ans += sum * a[id];
			if (ans >= H)break;
		}
		if (ans >= H)cout << -1 << endl;
		else cout << ans << endl;
	}
	return 0;
}

