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(:hcnt, :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
    hcnt = (Cs[i].lpt.to_f / hpt).ceil
    sum_dmg -= dmg if Cs[i].swf < Cs[0].swf
    arr << Btl.new(hcnt, dmg)
  end
  count = 0

  #cmp hcnt / dmg
  arr.sort{ |e0, e1| e0.hcnt * e1.dmg <=> e0.dmg * e1.hcnt }.reduce(sum_dmg) do |sum, b|
    count += b.hcnt
    res = sum + b.dmg * count
    break -1 if res >= Cs[0].lpt
    res
  end
end
 
p solve