N, M = gets.chomp.split(' ').map(&:to_i)

AB = []
N.times do |i|
  AB[i] = gets.chomp.split(' ').map(&:to_i)
end

group_by_day = AB.group_by{|v|v[0]}

day_reward = Array.new(M){[]}
group_by_day.each do |k, v|
  day_reward[k - 1] = v.map{|m| m[1]}
end

# print "\nday_reward =>"
# print day_reward
# print "\n--------------------"

@output = 0
M.times do |i|
  arr = day_reward[i].sort
  if arr.size == 0
    next
  end
  @output += arr.pop
  if i < (M - 1)
    day_reward[i + 1] += arr
  end
end

# print "\n@output=>"
print @output
