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)

class << ufs
  def right(l)
    return self.size if l >= self.size
    self[l] == l ? l : right(self[l])
  end

  def next_clear_index(l)
    r = right(l)
    r >= self.size ? nil : r
  end

  def extend_seq!(l)
    r = next_clear_index(l)
    self[l] = self[r] = r + 1 if r
  end
end

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

