// AOJ 2236
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0 ;i < (int)(n) ; i++)

#define SPD 0
#define HP 1
#define ATK 2
#define DEF 3
#define IDX 4

bool cmp(array<long long,2> a,array<long long,2> b){
	return a[0] * b[1] < a[1] * b[0];
}
int main(){
	int N;
	while( cin >> N && N ){
		N = N+1;
		vector< array<long long,5> > v(N);
		for(int i = 0 ; i < N; i++){
			cin >> v[i][HP] >> v[i][ATK] >> v[i][DEF] >> v[i][SPD];
			v[i][IDX] = i;
			v[i][SPD] *= -1;
		}
		long long fstHp = v[0][HP];
		sort(v.begin(),v.end());
		int my = -1;
		rep(i,N) if( v[i][IDX] == 0 ){ my = i;  break; }
		rep(i,my) v[my][HP] -= max(0ll,v[i][ATK]-v[my][DEF]);
		vector< array<long long,2> > kill;
		
		int error = 0;
		rep(i,N) if( i != my ){
			long long d = max(0ll,v[my][ATK]-v[i][DEF]);
			long long w = max(0ll,v[i][ATK]-v[my][DEF]);
			//cout << w << " " << d << endl;
			if( w == 0 ) continue;
			if( d == 0 ){ error = 1; break; }
			kill.push_back(array<long long,2>{w,(v[i][HP]+d-1)/d});
		}
		sort( kill.rbegin(), kill.rend(),cmp );
		//(a+c)b+c(b+d) < (a+c)d+a(b+d)
		//a*b+c*b+c*d < c*d+a*b+a*d
		//cb < ad
		//
		long long tot = 0;
		rep(i,kill.size()) tot += kill[i][0];
		rep(i,kill.size()){
			//cout << kill[i][0] << " " << kill[i][1] << endl;
			if( 1.*v[my][HP]-1.*tot*kill[i][1] <= -1 ){ error = 1; break; }
			v[my][HP] -= tot * kill[i][1] - kill[i][0];
			if( v[my][HP] < 0 ){ error = 1; break; }
			tot -= kill[i][0];
		}
		if(error){ cout << -1 << endl; continue; }
		else cout << fstHp - v[my][HP] << endl;
		
	}
}