N, M = gets.split.map(&:to_i)
abs  = N.times.map { gets.split.map(&:to_i) }
1.upto(N) do | id |
  abs[id-1] << id
end

ans = []
1.upto(M) do | i |
  #puts "i=#{i} ---------------"
  candi = abs.select{ |a, b, c| a <= i }
  next if candi.empty?
  best  = candi.max{ |(a1,b1,c1), (a2,b2,c2)| b1 <=> b2 }
  #p candi
  #p best
  ans << best[1]
  break if ans.size > N
  abs.delete(best)
end

a = 0
ans.each{ | e | a += e }
p a