Character = Struct.new(:lpt, :atk, :def, :swf)

@n = gets.to_i
Cs = []
(@n+1).times{ Cs << Character.new( *gets.split.map(&:to_i) ) }

Btl = Struct.new(:hpt, :dmg)

def solve
  sum_dmg = 0
  arr = []
  (1..@n).each do |i|
    dmg = [Cs[i].atk - Cs[0].def, 0].max
    hpt = [Cs[0].atk - Cs[i].def, 0].max
    return -1 if dmg > 0 && hpt == 0
    @next if hpt == 0
    hpt = (Cs[i].lpt.to_f / hpt).ceil
    sum_dmg -= hpt if Cs[i].swf > Cs[0].swf
    arr << Btl.new(hpt, dmg)
  end

  #cmp d/h
  arr.sort!{ |e0, e1| e1.hpt * e0.dmg <=> e1.dmg * e0.hpt }
  t = 0
  @n.times do |i|
    h, d = arr[i].hpt, arr[i].dmg
    t += d
    sum_dmg += h * t
    retur@n -1 if sum_dmg >= Cs[0].lpt
  end
  sum_dmg
end

p solve