#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


struct Enemy {
    ll hp;
    ll ap;
    ll sp;
    ll dp;
};

ostream& operator<<(ostream& os, const Enemy& v)
{
    os << "[hp:" << v.hp << ",ap:" << v.ap << ",sp:" << v.sp << ",dp:" << v.dp << "]" << endl;
    return os;
}

int main()
{
    ll n;
    cin >> n;

    ll H, A, D, S;
    cin >> H >> A >> D >> S;
    vector<Enemy> enemy;
    for (int i = 0; i < n; i++) {
        ll h, a, d, s;
        cin >> h >> a >> d >> s;
        if (a - D <= 0) {
            continue;
        }
        if (A - d <= 0) {
            cout << -1 << endl;
            return 0;
        }
        enemy.push_back(Enemy{
            h,
            max(a - D, 0LL),
            s - S,
            max(A - d, 0LL),
        });
    }
    const int size = enemy.size();
    //    show(enemy);
    vector<ll> turns(size);
    using P = pair<ll, ll>;
    vector<P> p(size);
    ll sum = 0;
    for (int i = 0; i < size; i++) {
        const auto& e = enemy[i];
        if (e.sp < 0) {
            sum -= e.ap;
        }
        const ll t = (e.hp + (e.dp) - 1) / e.dp;
        turns[i] = t;
        p[i] = make_pair(e.ap, t);
    }
    auto comp = [](const P& p1, const P& p2) -> bool {
        return p1.first * p2.second > p1.second * p2.first;
    };
    sort(p.begin(), p.end(), comp);
    //    show(p);
    vector<ll> tsum(size);
    for (int i = 0; i < size; i++) {
        if (i == 0) {
            tsum[i] = p[i].second;
        } else {
            tsum[i] = tsum[i - 1] + p[i].second;
        }
        sum += p[i].first * tsum[i];
        if (sum >= H) {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << sum << endl;

    return 0;
}