#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	int n;
	cin >> n;
	ll H, A, D, S;
	cin >> H >> A >> D >> S;
	ll ans = 0;
	vector<pii> v;
	REP(i, n)
	{
		ll h, a, d, s;
		cin >> h >> a >> d >> s;
		ll dmg = max(0ll, a - D);
		if (S > s)
		{
			H += dmg;
			ans -= dmg;
		}
		ll gdmg = max(0ll, A - d);
		if (dmg > 0 && gdmg == 0)
		{
			cout << -1 << endl;
			return 0;
		}
		if (dmg == 0) continue;
		v.emplace_back((h + gdmg - 1) / gdmg, dmg);
	}
	sort(ALL(v), [](const pii& a, const pii& b) {return (ld)a.first / a.second < (ld)b.first / b.second; });
	ll sum = 0;
	REP(i, v.size())
	{
		sum += v[i].first;
		ans += sum * v[i].second;
		if (ans >= H)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	cout << ans << endl;
	return 0;
}