#include <bits/stdc++.h>
using namespace std;
struct Data {
  long long h, a, d, s, t, id;
  bool operator<(const Data &data) const { return d * data.t > data.d * t; }
};
int n;
int main() {
  cin >> n;
  vector<Data> buf(n + 1);
  bool out = false;
  for (int i = 0; i < n + 1; i++) {
    buf[i].id = i;
    cin >> buf[i].h >> buf[i].a >> buf[i].d >> buf[i].s;
    if (i) {
      long long attack = max(0LL, buf[0].a - buf[i].d);
      if (attack == 0LL) {
        out = true;
        continue;
      }
      buf[i].t = (long long)(buf[i].h / attack) +
                 (long long)((buf[i].h % attack) != 0);
      assert(buf[i].t >= 0);
    }
  }
  if (out) {
    puts("-1");
    return 0;
  }
  sort(buf.begin(), buf.end());
  int sp = -1;
  for (int i = 0; i < buf.size(); i++)
    if (buf[i].id == 0) {
      sp = i;
      break;
    }
  long long ans = 0LL, total_turn = 0;
  for (int i = 0; i < buf.size(); i++) {
    if (i == sp) continue;
    long long attack = max(0LL, buf[i].a - buf[sp].d);
    if (buf[i].s < buf[sp].s) buf[i].t--;
    total_turn += buf[i].t;
    ans += attack * total_turn;
  }
  cout << ((ans < buf[sp].h) ? ans : -1) << endl;
  return 0;
}
