import heapq

import sys

input=sys.stdin.readline

N,M=list(map(int,input().split()))

lab=[]

for i in range(N):

    llab=list(map(int,input().split()))

    lab.append(llab)

lab.sort(key=lambda ab:ab[0])

hq=[]

ind=0

ans=0

#print(lab)

#c=0

for i in range(M):

    if hq:

            ans-=heapq.heappop(hq)

    for j in range(ind,N):

        #if f==0:

        #print(i,j,lab[j][0])

        if lab[j][0]==i+1:

            heapq.heappush(hq,-lab[j][1])#

        else:

            ind=j

            #j=N-1#これダメ、抜けられていない

            break

        #c+=1

    #if hq:

    #        ans-=heapq.heappop(hq)

    #print(hq)

#print(c)

if hq:

    ans-=heapq.heappop(hq)

print(ans)

#for i in range(3):

#    i=3

#    print(12345)

import heapq

import sys

input=sys.stdin.readline

N,M=list(map(int,input().split()))

lab=[]

for i in range(N):

    llab=list(map(int,input().split()))

    lab.append(llab)

lab.sort(key=lambda ab:ab[0])

hq=[]

ind=0

ans=0

for i in range(M):

    for j in range(ind,N):

        if lab[j][0]==i+1:

            heapq.heappush(hq,-lab[j][1])

        else:

            ind=j

            break

    if hq:

            ans-=heapq.heappop(hq)

print(ans)

import heapq
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
lab = []
for i in range(N):
    llab = list(map(int, input().split()))
    lab.append(llab)
lab.sort(key=lambda ab: ab[0])
hq = []
ind = 0
ans = 0
# print(lab)
# c=0
for i in range(M):
    if hq:
        ans -= heapq.heappop(hq)
    for j in range(ind, N):
        # if f==0:
        # print(i,j,lab[j][0])
        if lab[j][0] == i + 1:
            heapq.heappush(hq, -lab[j][1])  #
        else:
            ind = j
            # j=N-1#これダメ、抜けられていない
            break
        # c+=1
    # if hq:
    #        ans-=heapq.heappop(hq)
    # print(hq)
# print(c)
if hq:
    ans -= heapq.heappop(hq)
print(ans)
# for i in range(3):
#    i=3
#    print(12345)

import heapq
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
lab = []
for i in range(N):
    llab = list(map(int, input().split()))
    lab.append(llab)
lab.sort(key=lambda ab: ab[0])
hq = []
ind = 0
ans = 0
for i in range(M):
    for j in range(ind, N):
        if lab[j][0] == i + 1:
            heapq.heappush(hq, -lab[j][1])
        else:
            ind = j
            break
    if hq:
        ans -= heapq.heappop(hq)
print(ans)

-# print(lab) -# c=0 +    for j in range(ind, N): +        if lab[j][0] == i + 1: +            heapq.heappush(hq, -lab[j][1]) +        else: +            ind = j +            break -    for j in range(ind, N): -        # if f==0: -        # print(i,j,lab[j][0]) -        if lab[j][0] == i + 1: -            heapq.heappush(hq, -lab[j][1])  # -        else: -            ind = j -            # j=N-1#これダメ、抜けられていない -            break -        # c+=1 -    # if hq: -    #        ans-=heapq.heappop(hq) -    # print(hq) -# print(c) -if hq: -    ans -= heapq.heappop(hq) -# for i in range(3): -#    i=3 -#    print(12345)