#include <bits/stdc++.h>
using namespace std;
int N;
const int MAX = 40000;
int orderS[MAX];
int orderA[MAX];
class Data {
 public:
  int h, a, d, s, id;
  long long int c;
  void attack(Data& data) { data.h -= max(a - data.d, 0); }
  bool isDead() {
    if (h < 0)
      return true;
    else
      return false;
  }
};
vector<Data> vec;
void input() {
  N++;
  for (int i = 0; i < N; i++) {
    Data data;
    cin >> data.h >> data.a >> data.d >> data.s;
    data.id = i;
    vec.push_back(data);
  }
}
void init() { vec.clear(); }
bool cmpS(Data a, Data b) { return a.s > b.s; }
bool cmpA(Data a, Data b) { return a.c > b.c; }
void makeOrderS() {
  vector<Data> vec2 = vec;
  sort(vec2.begin(), vec2.end(), cmpS);
  for (int i = 0; i < N; i++) orderS[i] = vec2[i].id;
}
int makeOrderA(long long int sum) {
  vector<Data> vec2 = vec;
  for (int i = 1; i < N; i++) {
    int damage1 = max(vec[0].a - vec[i].d, 0);
    int damage2 = max(vec[i].a - vec[0].d, 0);
    if (sum == 0 && damage1 == 0) return 2;
    if (damage1 == 0) return 1;
    int turn = vec[0].h / damage1;
    vec2[i].c = (turn * sum - damage2);
    sum -= damage2;
  }
  sort(vec2.begin(), vec2.end(), cmpA);
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (vec2[i].id == 0) continue;
    orderA[cnt++] = vec2[i].id;
  }
  return false;
}
int solve() {
  init();
  input();
  long long int sum = 0;
  for (int i = 1; i < N; i++) {
    sum += max(vec[i].a - vec[0].d, 0);
  }
  makeOrderS();
  int tmp = makeOrderA(sum);
  if (tmp == 1) {
    return -1;
  } else if (tmp == 2) {
    return 0;
  }
  int h = vec[0].h;
  for (int i = 0; i < N; i++) {
    int id = orderS[i];
    if (id == 0) break;
    vec[id].attack(vec[0]);
  }
  if (vec[0].isDead()) return -1;
  int a_id = 0;
  while (1) {
    if (a_id == N - 1) break;
    if (vec[0].isDead()) return -1;
    int e_id = orderA[a_id];
    int damage1 = max(vec[0].a - vec[e_id].d, 0);
    int damage2 = max(vec[e_id].a - vec[0].d, 0);
    if (damage2 == 0) return -1;
    int turn = vec[e_id].h / damage1;
    vec[0].h -= (turn * sum - damage2);
    sum -= damage2;
    a_id++;
  }
  return h - vec[0].h;
};
int main() {
  while (cin >> N) {
    cout << solve() << endl;
  }
  return 0;
}
