#include <bits/stdc++.h>
struct person_dat {
  int h;
  int a;
  int d;
  int s;
  int delete_flag;
  int need_turn;
} person[40000 + 1];
int N;
int damage_per_turn;
int total_damage;
int rest_time;
int cannot_win;
int get_enemy(int turn) {
  int i, maxd, maxi, d;
  maxd = -1;
  for (i = 1; i <= N; i++) {
    if (person[i].delete_flag) continue;
    if (person[i].a <= person[0].d)
      d = 0;
    else if (person[i].s > person[0].s)
      d = (rest_time + 1 - person[i].need_turn) * (person[i].a - person[0].d);
    else
      d = (rest_time - person[i].need_turn) * (person[i].a - person[0].d);
    if (d > maxd) {
      maxd = d;
      maxi = i;
    }
  }
  person[maxi].delete_flag = -1;
  return (maxi);
}
int main() {
  int i, next_enemy, hosei, current_damage;
  scanf("%d", &N);
  cannot_win = 0;
  rest_time = 0;
  for (i = 0; i < N; i++) {
    scanf("%d %d %d %d", &person[i].h, &person[i].a, &person[i].d,
          &person[i].s);
    if (i != 0) {
      if (person[0].a <= person[i].d) {
        cannot_win = -1;
        person[i].need_turn = -1;
        continue;
      } else {
        person[i].need_turn = person[i].h / (person[0].a - person[i].d);
        person[i].need_turn +=
            (person[i].h % (person[0].a - person[i].d) ? 1 : 0);
        rest_time += person[i].need_turn;
      }
    }
  }
  if (cannot_win) {
    total_damage = -1;
    goto END;
  }
  total_damage = 0;
  damage_per_turn = 0;
  for (i = 1; i < N; i++)
    damage_per_turn +=
        ((((person[i].a - person[0].d)) > (0)) ? ((person[i].a - person[0].d))
                                               : (0));
  for (i = 1; i < N; i++) {
    next_enemy = get_enemy(i);
    if (person[next_enemy].s > person[0].s)
      hosei = (person[next_enemy].a - person[0].d);
    else
      hosei = 0;
    current_damage = (person[next_enemy].need_turn * damage_per_turn - hosei);
    total_damage += current_damage;
    if (total_damage >= person[0].h) {
      total_damage = -1;
      goto END;
    }
    damage_per_turn -= (person[next_enemy].a - person[0].d);
    rest_time -= person[next_enemy].need_turn;
  }
END:
  printf("%d\n", total_damage);
  return (0);
}
