from heapq import heappop, heappush

from bisect import bisect

from collections import deque

inf = 10**5





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

info = [list(map(int,input().split())) for _ in range(N)]

info = deque(sorted(info))



ans = 0

hq = []



for i in range(M-1,-1,-1):

    index = bisect(info, [M-i,inf])

    for _ in range(index):

        A, B = info.popleft()

        heappush(hq, -B)

    if hq:

        B = -heappop(hq)

        ans += B



print(ans)
from heapq import heappush, heappop



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



# works[i] : M-i日に報酬が得られるリスト

works = [[] for _ in range(10**6)]



for _ in range(N):

    A, B = list(map(int,input().split()))

    # 報酬が得られる日付に応じて格納

    works[A].append(B)



hq = []

ans = 0



for i in range(1,M+1):

    # M-i日から降順に仕事の候補リストを作る

    for w in works[i]:

        # 大きいものから選びたいのでマイナスをかける

        heappush(hq, -w)

    # M-i日の時点で報酬が最大の仕事を選ぶ

    if hq:

        ans += -heappop(hq)



print(ans)
from heapq import heappop, heappush
from bisect import bisect
from collections import deque

inf = 10**5
N, M = list(map(int, input().split()))
info = [list(map(int, input().split())) for _ in range(N)]
info = deque(sorted(info))
ans = 0
hq = []
for i in range(M - 1, -1, -1):
    index = bisect(info, [M - i, inf])
    for _ in range(index):
        A, B = info.popleft()
        heappush(hq, -B)
    if hq:
        B = -heappop(hq)
        ans += B
print(ans)

from heapq import heappush, heappop

N, M = list(map(int, input().split()))
# works[i] : M-i日に報酬が得られるリスト
works = [[] for _ in range(10**6)]
for _ in range(N):
    A, B = list(map(int, input().split()))
    # 報酬が得られる日付に応じて格納
    works[A].append(B)
hq = []
ans = 0
for i in range(1, M + 1):
    # M-i日から降順に仕事の候補リストを作る
    for w in works[i]:
        # 大きいものから選びたいのでマイナスをかける
        heappush(hq, -w)
    # M-i日の時点で報酬が最大の仕事を選ぶ
    if hq:
        ans += -heappop(hq)
print(ans)

-from heapq import heappop, heappush -from bisect import bisect -from collections import deque +from heapq import heappush, heappop -inf = 10**5 -info = [list(map(int, input().split())) for _ in range(N)] -info = deque(sorted(info)) +# works[i] : M-i日に報酬が得られるリスト +works = [[] for _ in range(10**6)] +for _ in range(N): +    A, B = list(map(int, input().split())) +    # 報酬が得られる日付に応じて格納 +    works[A].append(B) +hq = [] -hq = [] -for i in range(M - 1, -1, -1): -    index = bisect(info, [M - i, inf]) -    for _ in range(index): -        A, B = info.popleft() -        heappush(hq, -B) +for i in range(1, M + 1): +    # M-i日から降順に仕事の候補リストを作る +    for w in works[i]: +        # 大きいものから選びたいのでマイナスをかける +        heappush(hq, -w) +    # M-i日の時点で報酬が最大の仕事を選ぶ -        B = -heappop(hq) -        ans += B +        ans += -heappop(hq)