class PriorityQueue
  def initialize(array: [], order: :desc)
    @data = []
    @order = order
    array.each{ |a| add(a) }
  end

  def add(element)
    @data.push(element)
    bottom_up
  end

  def peek
    @data[0]
  end

  def poll
    return 0 if size == 0
    return @data.shift if size == 1

    @data.shift
  end

  def size
    @data.size
  end

  private def should_be_swapped?(a, b)
    @order == :desc ? a < b : a > b
  end

  private def swap(i, j)
    @data[i], @data[j] = @data[j], @data[i]
  end

  private def bottom_up
    (size - 1).downto(0) do |i|
      parent_idx = parent_idx(i)
      return unless should_be_swapped?(@data[parent_idx], @data[i])
      swap(parent_idx, i)
    end
  end

  private def parent_idx(idx)
    (idx - (idx.even? ? 2 : 1)) / 2
  end
end

n, m = gets.chomp.split(' ').map(&:to_i)
jobs = (1..n).map { gets.chomp.split(' ').map(&:to_i) }.group_by { |ab| ab.first }
pq = PriorityQueue.new
sum = 0
1.upto(m) do |d|
  jobs[d].map(&:last).each { |money| pq.add(money) } if jobs[d]
  sum += pq.poll
end

puts sum
