#include <bits/stdc++.h>
using namespace std;
struct DATA {
  double h, a, d, s;
};
DATA make(double h, double a, double d, double s) {
  DATA data;
  data.h = h;
  data.a = a;
  data.d = d;
  data.s = s;
  return data;
}
bool compare(const DATA& x, const DATA& y) {
  if (x.a != y.a) return x.a > y.a;
  return x.s > y.s;
}
double solve(vector<DATA> data, DATA main, double high, double low) {
  sort(data.begin(), data.end(), compare);
  double max = main.h;
  while (1) {
    main.h -= high;
    if (main.h <= 0) return -1;
    data[0].h -= main.a - data[0].d;
    if (data[0].h <= 0) {
      if (data.size() == 1) return max - main.h;
      if (data[0].s > main.s)
        high -= data[0].a - main.d;
      else
        low -= data[0].a - main.d;
      data.erase(data.begin());
    }
    main.h -= low;
    if (main.h <= 0) return -1;
  }
}
int main() {
  int i, n, ans;
  double h, a, d, s, high = 0, low = 0;
  DATA main;
  vector<DATA> data;
  cin >> n;
  cin >> h >> a >> d >> s;
  main = make(h, a, d, s);
  for (i = 0, ans = 0; i < n; ++i) {
    cin >> h >> a >> d >> s;
    if (d >= main.a) ans = -1;
    if (ans == -1) continue;
    if (a <= main.d) continue;
    data.push_back(make(h, a, d, s));
    if (s > main.s)
      high += a - main.d;
    else
      low += a - main.d;
  }
  if (ans == -1)
    cout << ans << endl;
  else
    cout << (int)solve(data, main, high, low) << endl;
  return 0;
}
