#include <bits/stdc++.h>
#define N 40001
using namespace std;
typedef long long ll;
struct po{ll h,a,d,s;};

ll n;
po A[N];
ll hp,a,d,s,sum;
bool compare(po x,po y){
  if(a-x.d<=0||a-y.d<=0)return 0;

  ll cnt1=1+(x.h-1)/(a-x.d),cnt2=1+(y.h-1)/(a-y.d);
  ll X=cnt1*(x.a-d)+(cnt1+cnt2)*(y.a-d);
  ll Y=cnt2*(y.a-d)+(cnt1+cnt2)*(x.a-d);
  return X<Y;
}


ll solve(){
  sort(A,A+n,compare);
  for(int i=0;i<n;i++){
    po t=A[i];
    if(hp<=0)return -1;
    if(a-t.d<=0) return -1;
    ll cost=((t.h-1)/(a-t.d))*sum;
    sum-=t.a-d;
    hp-=(cost+sum);
    
  }
  if(hp<=0)return -1;
  return hp;
}

int main(){
  cin>>n;
  cin>>hp>>a>>d>>s;
  
  ll ofs=0;
  for(int i=0;i<n;i++){
    po &t=A[i];
    cin>>t.h>>t.a>>t.d>>t.s;
    if(t.a-d<=0){n--,i--;continue;};
    if(t.s>s) ofs+=(t.a-d);
    sum+=(t.a-d);
  }
  
  ll H = hp;
  hp-=ofs;
  ll ans= H-solve();
  if(H<ans)cout<<-1<<endl;
  else cout<<ans<<endl;

  return 0;
}