/*
AizuOnline A2236
Title
*/
#include <stdio.h>
#define MAXPERSON 40000
#define max(x,y) (((x)>(y))?(x):(y))

struct person_dat
{
        int h;
        int a;
        int d;
        int s;
        int delete_flag;
        int need_turn;
} person[MAXPERSON+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);
}
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)  // no damege
                        {
                                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 += max((person[i].a - person[0].d),0); 
#ifdef DEBUG
        printf("DT:%d Rt:%d\n",damage_per_turn,rest_time);
#endif
        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;
#ifdef DEBUG
                printf("HOS:%d Es:%d Mys:%d\n",hosei,person[next_enemy].s , person[0].s);
#endif

                current_damage=(person[next_enemy].need_turn * damage_per_turn - hosei);
#ifdef DEBUG
                printf("DMG %d (%d)NTurn %d\n",
                       damage_per_turn,person[next_enemy].need_turn,current_damage);
#endif
                total_damage += current_damage; 
                if(total_damage >= person[0].h)
                {
                        total_damage=-1; // LOSE
                        goto END;
                }

                damage_per_turn -= (person[next_enemy].a - person[0].d);
                rest_time -= person[next_enemy].need_turn;
#ifdef DEBUG
                printf("TOTAL DAMAGE %d\n",total_damage);
#endif
        }
        END:
                printf("%d\n",total_damage);
        
return(0);
}