integer N,M,x,y,maxhi,tuyo
integer,allocatable,dimension(:,:)::A
integer,allocatable,dimension(:)::B
integer(16)ans
read*,N,M
allocate(A(1:M,0:10000),B(M))
A=0
B=0
do i=1,N
  read*,x,y
  if(x<=M)then
    B(x)=B(x)+1
    A(x,B(x))=y
  endif
end do
ans=0
do i=1,M
  if(B(i)/=0)then
    call Heapsort(B(i),A(i,1:B(i)))
  end if
  maxhi=0
  do j=1,i
    if(maxhi<A(j,B(j)))then
      maxhi=A(j,B(j))
      tuyo=j
    endif
  end do
  if(maxhi/=0)then
    B(tuyo)=B(tuyo)-1
  endif
  ans=ans+maxhi
end do

print"(I0)",ans
contains
subroutine heapsort(n,array)
  implicit none
!ここの入力は状況に応じて変更すること
  integer,intent(in) :: n
  integer,intent(inout) :: array(1:n)
  integer::i,k,j,l
  integer:: t

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j<=k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
  enddo
  return
end subroutine heapsort
end