// BEGIN CUT HERE
#ifdef _MSC_VER
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <array>
#include <climits>
#include <bitset>
#include <cassert>
#include <unordered_map>
#include <agents.h>
#else
// END CUT HERE
#include <bits/stdc++.h>
// BEGIN CUT HERE
#endif
// END CUT HERE

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;


int main(){
	int n;
	cin >> n;
	vector<int> h(n + 1), a(n + 1), d(n + 1), s(n + 1);
	rep(i, n + 1) cin >> h[i] >> a[i] >> d[i] >> s[i];

	ll ans = 0;
	ll sum = 0;
	vector<pair<int, int>> e;
	FOR(i, 1, n + 1){
		int dmg = a[i] - d[0];
		if (dmg <= 0) continue;
		if (s[i] > s[0]) ans += dmg;
		if (d[i] >= a[0]){
			cout << -1 << endl;
			return 0;
		}
		int x = a[0] - d[i];
		int turn = (h[i] + x - 1) / x;

		e.push_back(MP(dmg, turn));
		sum += dmg;
	}
	if (sum >= 2e9){
		cout << -1 << endl;
		return 0;
	}
	sort(ALL(e), [](const pair<int, int> &lhs, const pair<int, int> &rhs){return 1.*lhs.second / lhs.first < 1.*rhs.second / rhs.first; });

	for (auto p : e){
		ans += sum*p.second - p.first;
		if (ans >= h[0]){
			ans = -1;
			break;
		}
		sum -= p.first;
	}

	cout << ans << endl;
}