#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#define F first
#define S second
using namespace std;
int n;
//long long int ph,pa,pd,ps;
long long int h[44444];
long long int a[44444];
long long int b[44444];
long long int s[44444];
pair<long double,int> p[44444];
long long int ad;
int main(void){
  cin >> n /*>> ph >> pa >> pd >> ps*/;
  for(int i = 0; i <= n; i++){
    cin >> h[i] >> a[i] >> b[i] >> s[i];
  }

  for(int i = 1; i <= n; i++){

    ad += max(a[i] - b[0],0LL);

    if(a[0] - b[i] <= 0){
      p[i-1].F = -1;
    }else{
      p[i-1].F = (long double)max(a[i] - b[0],0LL) / (long long int)((h[i]+a[0]-b[i]-1)/(a[0]-b[i]));
    }
    p[i-1].S = i;
  }
  sort(p,p+n,greater< pair<long double,int> >());

  long long int d = 0;
  for(int i = 0; i < n; i++){
    int pp = p[i].S;
    if(p[i].F >= 0){
      d += ad * ((h[pp]+a[0]-b[pp]-1)/(a[0]-b[pp]));
      if(s[0] > s[pp]) d -= max(a[pp] - b[0],0LL);
    }else if(max(a[pp]-b[0],0LL)){
      d += h[0];
    }
    if(h[0] <= d){
      cout << -1 << endl;
      return 0;
    }
    //cout << pp << endl;
    ad -= max(a[pp] - b[0],0LL);
  }
  cout << d << endl;
}