#include <bits/stdc++.h>
using namespace std;
bool cmp(array<long long, 2> a, array<long long, 2> b) {
  return a[0] * b[1] < a[1] * b[0];
}
int main() {
  int N;
  while (cin >> N && N) {
    N = N + 1;
    vector<array<long long, 5> > v(N);
    for (int i = 0; i < N; i++) {
      cin >> v[i][1] >> v[i][2] >> v[i][3] >> v[i][0];
      v[i][4] = i;
      v[i][0] *= -1;
    }
    long long fstHp = v[0][1];
    sort(v.begin(), v.end());
    int my = -1;
    for (int i = 0; i < (int)(N); i++)
      if (v[i][4] == 0) {
        my = i;
        break;
      }
    for (int i = 0; i < (int)(my); i++)
      v[my][1] -= max(0ll, v[i][2] - v[my][3]);
    vector<array<long long, 2> > kill;
    int error = 0;
    for (int i = 0; i < (int)(N); i++)
      if (i != my) {
        long long d = max(0ll, v[my][2] - v[i][3]);
        long long w = max(0ll, v[i][2] - v[my][3]);
        if (w == 0) continue;
        if (d == 0) {
          error = 1;
          break;
        }
        kill.push_back({w, (v[i][1] + d - 1) / d});
      }
    sort(kill.rbegin(), kill.rend(), cmp);
    long long tot = 0;
    for (int i = 0; i < (int)(kill.size()); i++) tot += kill[i][0];
    for (int i = 0; i < (int)(kill.size()); i++) {
      if (1. * v[my][1] - 1. * tot * kill[i][1] <= -1) {
        error = 1;
        break;
      }
      v[my][1] -= tot * kill[i][1] - kill[i][0];
      if (v[my][1] < 0) {
        error = 1;
        break;
      }
      tot -= kill[i][0];
    }
    if (error) {
      cout << -1 << endl;
      continue;
    } else
      cout << fstHp - v[my][1] << endl;
  }
}
