#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}


template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

struct enemy {
    ll time, damage;
    enemy(ll _time, ll _damage) {
        time = _time;
        damage = _damage;
    }
};

bool operator<(enemy a, enemy b) {
    return a.time * b.damage < b.time * a.damage;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
ll N;
ll h[40500], a[40500], d[40500], s[40500];
vector<enemy> v;
ll Total;

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    for(int i = 0; i <= N; i++) {
        cin >> h[i] >> a[i] >> d[i] >> s[i];
    }
    for(int i = 1; i <= N; i++) {
        ll damage = a[i] - d[0];
        if(damage <= 0) continue;
        if(s[i] > s[0]) Total += damage;
        ll dam = a[0] - d[i];
        if(dam <= 0) {
            cout << -1 << endl;
            return 0;
        }
        ll timer = (h[i] + dam - 1) / dam;
        v.emplace_back(timer, damage);
    }
    sort(v.begin(), v.end());
    vector<ll> sum(v.size() + 1);
    for(int i = (int)v.size() - 1; i >= 0; i--) {
        sum[i] = sum[i+1] + v[i].damage;
    }
    for(int i = 0; i < v.size(); i++) {
        Total += (v[i].time - 1) * sum[i];
        Total += sum[i+1];
        if(Total >= h[0]) {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << Total << endl;
    return 0;
}
