#include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> h, a, d, s;
inline 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) * max(a[e2] - d[0], 0LL) < T(e2) * max(a[e1] - d[0], 0LL);
}
int main() {
  while (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[0] - d[i] <= 0) {
          if (a[i] - d[0] <= 0) {
            continue;
          } else {
            lose = true;
            break;
          }
        }
        id.push_back(i);
      }
      if (lose) {
        cout << -1 << endl;
        continue;
      }
    }
    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) * max(a[e] - d[0], 0LL);
      else
        damage += turn * max(a[e] - d[0], 0LL);
    }
    if (h[0] - damage <= 0)
      cout << -1 << endl;
    else
      cout << damage << endl;
  }
  return 0;
}
