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

typedef long long ll;

struct at{
  ll a,t;
};

bool comp(const at &x, const at &y){
  return (x.a*(x.t-1)+y.a*(x.t+y.t-1))<(y.a*(y.t-1)+x.a*(x.t+y.t-1));
}

vector<at> e;

int main(){
  int n;
  cin>>n;
  ll hp,atk,def,sp;
  ll ans=0;
  cin>>hp>>atk>>def>>sp;
  for(int i=0;i<n;i++){
    ll ehp,eatk,edef,esp;
    cin>>ehp>>eatk>>edef>>esp;
    ll dm=max((ll)0,atk-edef);
    ll dmk=max(0LL,eatk-def);
    if(esp>sp)ans+=dmk;
    
    if(dm==0 && dmk>0){
      cout<<-1<<endl;
      exit(0);
    }

    if(dm==0)continue;
    e.push_back((at){dmk,(ehp+dm-1)/dm});
  }

  sort(e.begin(),e.end(),comp);
  ll cnt=0;
  for(int i=0;i< (int)e.size() ;i++){
    ll x=e[i].a,y=e[i].t;
    cnt+=y;
    ans+=x*(cnt-1);
    if(hp-ans<=0) break;
  }
  if(hp-ans<=0) cout<<-1<<endl;
  else cout<<ans<<endl;
  return 0;
}