#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long lli;

const int MAXN = 40004;
int n;
lli h[MAXN], a[MAXN], d[MAXN], s[MAXN];

lli T(const int &e) {
  return (h[e]+(a[0]-d[e]-1))/(a[0]-d[e]);
}

bool comp(const int &e1, const int &e2) {
  return T(e1)*(a[e2]-d[0]) < T(e2)*(a[e1]-d[0]);
}

int main() {
  while(cin >> n) {
    ++n;
    for(int i = 0; i < n; ++i) {
      cin >> h[i] >> a[i] >> d[i] >> s[i];
    }
    vector<int> id;
    bool lose = false;
    for(int i = 1; i < n; ++i) {
      if(a[i]-d[0] <= 0) {
	continue;
      }
      if(a[0]-d[i] <= 0) {
	lose = true;
	break;
      }
      id.push_back(i);
    }
    if(lose) {
      cout << -1 << endl;
      continue;
    }
    sort(id.begin(), id.end(), comp);
    lli damage = 0;
    lli turn = 0;
    for(int i = 0; i < id.size(); ++i) {
      int e = id[i];
      turn += T(e);
      if(s[0] > s[e])
	damage += (turn-1)*(a[e]-d[0]);
      else
	damage += turn*(a[e]-d[0]);
      damage = min(damage, h[0]);
    }
    if(h[0]-damage <= 0) cout << -1 << endl;
    else cout << damage << endl;
  }
  return 0;
}