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]}.sort
end

# print day_reward

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

print @output
