local function meld(a,b)
    if not a then
        return b
    elseif not b then
        return a
    elseif a[1]<b[1] then
        b[4]=a[3]
        a[3]=b
        return a
    else
        a[4]=b[3]
        b[3]=a
        return b
    end
end

local function pair(a)
    local b=a and a[4]
    local c,d,e
    while b do
        c=b[4]
        a[4]=false
        b[4]=false
        a=meld(a,b)
        a[4]=d
        d=a
        a=c
        b=a and a[4]
    end
    while d do
        e=d
        d=d[4]
        a=meld(a,e)
    end
    return a
end

local PairingHeap={}

PairingHeap.empty=function(self)
    return not self[1]
end

PairingHeap.push=function(self,key,value)
    self[1]=meld(self[1],{key,value,false,false})
end

PairingHeap.top=function(self)
    return self[1][2]
end

PairingHeap.pop=function(self)
    self[1]=pair(self[1][3])
end

PairingHeap.join=function(self,other)
    self[1]=meld(self[1],other[1])
    return self
end

PairingHeap.new=function(self)
    local obj={false}
    setmetatable(obj,{__index=PairingHeap})
    return obj
end

----------

local n,m=io.read("n","n")
local t={}
for i=1,m do
    t[i]={}
end
for i=1,n do
    local a,b=io.read("n","n")
    if a<m+1 then
        table.insert(t[a],b)
    end
end

local pq=PairingHeap:new()
local total=0
for i=1,m do
    for j=1,#t[i] do
        local b=t[i][j]
        pq:push(-b,b)
    end
    if not pq:empty() then
        total=total+pq:top()
        pq:pop()
    end
end
print(total)