#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

#define dump(a) (cerr << #a << " = " << (a) << endl)

struct data {
	int turn, damage, idx;
	data(int t, int d, int i):turn(t), damage(d), idx(i){}

	bool operator<(const data& d) const {
		return (double)damage / turn < (double)d.damage / d.turn;
	}
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	vector<int> h(n + 1), a(n + 1), d(n + 1), s(n + 1);
	for(int i = 0; i < n + 1; ++i)
		cin >> h[i] >> a[i] >> d[i] >> s[i];

	vector<data> dat;
	dat.reserve(n);
	for(int i = 1; i < n + 1; ++i) {
		const int attack = max(0, a[0] - d[i]);
		const int damage = max(0, a[i] - d[0]);
		if(attack == 0) {
			if(damage == 0)
				continue;

			cout << -1 << endl;
			return EXIT_SUCCESS;
		}

		const int turn = ceil((double)h[i] / attack);
		dat.push_back(data(turn, damage, i));
	}

	sort(dat.rbegin(), dat.rend());
	long long turn = 0, hp = h[0];
	for(int i = 0; i < n; ++i) {
		turn += dat[i].turn;
		hp -= dat[i].damage * (turn - (s[0] > s[dat[i].idx] ? 1 : 0));
		if(hp <= 0) {
			cout << -1 << endl;
			return EXIT_SUCCESS;
		}
	}

	cout << h[0] - hp << endl;

	return EXIT_SUCCESS;
}