#include<bits/stdc++.h>
using namespace std;

struct info{
    long long i, a, m;
    double key;
    info(long long i, long long a, long long m, double key):i(i),a(a),m(m),key(key){}
    info(){}
    bool operator<(const info& aa) const{ return key < aa.key; }
    bool operator>(const info& aa) const{ return key > aa.key; }
};

int main(){

    int n; cin >> n;
    long long rh, ra, rd, rs; cin >> rh >> ra >> rd >> rs;
    long long H = rh;
    vector<info> array(n);

    long long sum_a = 0LL;
    for(int i = 0; i < n; i++){
        long long h, a, d, s; cin >> h >> a >> d >> s;
        if(s > rs) rh -= max(0LL, a - rd);
        //cout << ra - d << endl;
        if(ra - d <= 0 && a - rd > 0){

            for(int j = i + 1; j < n; j++) cin >> h >> a >> d >> s;
          	//cout << "hoge" << endl;

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

        if(ra - d > 0) array[i] = info(i, max(0LL, a - rd), (h + (ra - d) - 1) / (ra - d), 1);
        else array[i] = info(i, 0, 0, 0);
        sum_a += array[i].a;
    }

    for(int i = 0; i < n; i++){
		if(array[i].key == 0) continue;
        array[i].key = (double)array[i].a / array[i].m;
    }

    sort(array.begin(), array.end());
    reverse(array.begin(), array.end());
    
    for(int i = 0; i < n; i++){
		//cout << "i= " << i << "rh= " << rh << " array[i].a = " << array[i].a << " array[i].m = " << array[i].m << " array[i].key = " << array[i].key << endl;
        rh -= (array[i].m - 1) * sum_a;
        if(rh < 0){
			//cout << "huhuhuhga" << endl;
            cout << -1 << endl;
            return 0;
        }

        sum_a -= array[i].a;
        rh -= sum_a;
    }

    //cout << rh << endl;
    if(rh <= 0){
		//cout << "piyo" << endl;
        cout << -1 << endl;
        return 0;
    }else{
        cout << H - rh << endl;
    }
    return 0;
}

