#include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> h, a, d, s;
long long 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() {
  cin >> n;
  ++n;
  h.resize(n);
  a.resize(n);
  d.resize(n);
  s.resize(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;
      return 0;
    }
  }
  sort(id.begin(), id.end(), comp);
  long long damage = 0;
  long long 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]);
  }
  if (h[0] - damage <= 0)
    cout << -1 << endl;
  else
    cout << damage << endl;
  return 0;
}
