#include <bits/stdc++.h>
using namespace std;
const long long MAX_N = 100005;
struct data {
  long long h, a, d, s, u, v;
};
vector<data> vec;
data zibun;
bool possible(long long cri) {
  long long tm = 0;
  long long al = 0;
  for (long long i = 0; i < (long long)((long long)(vec).size()); ++i) {
    if (zibun.s > vec[i].s) {
      tm += vec[i].v - 1;
      al += tm * vec[i].u;
      ++tm;
      if (al > cri) {
        return false;
      }
    } else {
      tm += vec[i].v;
      al += tm * vec[i].u;
      if (al > cri) {
        return false;
      }
    }
  }
  return true;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (long long i = 0; i < (long long)(n + 1); ++i) {
    long long w, x, y, z;
    cin >> w >> x >> y >> z;
    if (i == 0) {
      zibun = (data){w, x, y, z, 0LL, 0LL};
    } else {
      if (zibun.a <= y) {
        if (zibun.d < x) {
          cout << "-1\n";
          return 0;
        } else {
          continue;
        }
      }
      vec.push_back(
          (data){w, x, y, z, max(x - zibun.d, 0LL),
                 (long long)floor((double)(w - 0.5) / (zibun.a - y)) + 1LL});
    }
  }
  sort((vec).begin(), (vec).end(),
       [&](const data& x, const data& y) { return y.u * x.v < x.u * y.v; });
  long long l = 0, r = zibun.h;
  if (!possible(r)) {
    cout << "-1\n";
    return 0;
  }
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (possible(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << "\n";
  return 0;
}
