N, M = gets.split.map(&:to_i)
S = N.times.map { gets.split.map(&:to_i) }.sort_by{|v| -v[1] }
ufs = Array.new(M + 1, &:itself) # a variant of Union Find Tree

class << ufs
  def next_empty_seq(l)
    loop do
      if l >= self.size
        return nil
      elsif l == self[l]
        return self[l]
      else
        l = self[l]
      end
    end
  end

  def extend_seq!(l)
    r = next_empty_seq(l)
    return nil unless r
    r_ = r + 1
    loop do
      break if l >= r_
      m = self[l]
      self[l] = r_
      l = m
    end
    r_
  end
end

puts S.inject(0){|s, (a, b)| ufs.extend_seq!(a) ? s + b : s }

