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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

struct enemy{
  ll time,attack;
};
bool sorter(const enemy& a,const enemy& b){
  return a.time*b.attack<b.time*a.attack;
}

ll n;
ll h[40040],a[40040],d[40040],s[40040];
enemy es[40040];

int main(){
	cin.sync_with_stdio(false);
  cin>>n;
  n++;
  rep(i,n)cin>>h[i]>>a[i]>>d[i]>>s[i];
  repl(i,1,n){
    ll damege=mmax(0,a[0]-d[i]);
    ll attack=mmax(0,a[i]-d[0]);
    if(damege==0&&attack>0){
      cout<<-1<<endl;
      return 0;
    }
    if(damege!=0)es[i].time=(h[i]+damege-1)/damege;
    else es[i].time=1;
    es[i].attack=attack;
  }
  ll res=0,sum=0;
  repl(i,1,n){
    if(s[i]>s[0]){
      res+=es[i].attack;
    }
    sum+=es[i].attack;
  }

  sort(es+1,es+n,sorter);
  repl(i,1,n){
    res+=sum*(es[i].time-1)+(sum-es[i].attack);
    if(res>=h[0]){
      cout<<-1<<endl;
      return 0;
    }
    sum-=es[i].attack;
  }
  if(res>=h[0])cout<<-1<<endl;
  else cout<<res<<endl;
	return 0;
}