#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i,s,e) for(int i = (s);i <= (e);i++)

int n;

i64 h[40404];
i64 a[40404];
i64 d[40404];
i64 s[40404];
i64 A[40404];
i64 B[40404];

i64 ceil(i64 A,i64 B){
    return A / B + min(1LL,A % B);
}

int main(){
    cin >> n;
    vector<int> vec;
    rep(i,0,n){
        cin >> h[i] >> a[i] >> d[i] >> s[i];
        if(i == 0) continue;

        //cant
        if(a[0] <= d[i] && a[i] > d[0]){
            cout << -1 << endl;
            return 0;
        }
        if(a[0] > d[i]){
            vec.push_back(i);
            A[i] = (ceil(h[i],a[0] - d[i]));
            B[i] = (max(0LL,a[i] - d[0]));
        }
    }

    auto ope = [&](int i,int j){
        return A[i] * B[j] < A[j] * B[i];
    };

    sort(vec.begin(),vec.end(),ope);

    i64 HP = 0;
    i64 cou = 0;

    rep(i,0,(int)vec.size() - 1){
        int index = vec[i];
        cou += A[index];
        HP += cou * B[index];
        if(s[index] < s[0]) HP -= B[index];
        if(h[0] <= HP){
            cout << -1 << endl;
            return 0;
        }
    }

    cout << HP << endl;
    return 0;
}

