import heapq # 優先度付きキュー(最小値取り出し)

n,m = list(map(int, input().split()))

a = [list(map(int,input().split())) for i in range(n)]

a.sort(reverse = True)

b = []

heapq.heapify(b)   # ヒープ化

ans = 0



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

    while a and a[-1][0] == i:

        val = a.pop()[1]

        heapq.heappush(b, (-1)*(val))           # 今考えてる日にちの価値をpush

    if b:

        val = heapq.heappop(b)*(-1)             # 最大値の取り出し

        ans += val

print(ans)
import heapq # 優先度付きキュー(最小値取り出し)



inf = 10**15

mod = 10**9+7

n,m = list(map(int, input().split()))

ab = [list(map(int, input().split())) for i in range(n)]

ab.sort(key = lambda x:x[0], reverse = True)

q = []

heapq.heapify(q)   # ヒープ化

ans = 0

for i in range(1,m+1): # m-i日目からスタートして間に合う仕事

    while ab and ab[-1][0] == i:

        _,val = ab.pop()

        heapq.heappush(q, (-1) * val)

    if q:

        ans += heapq.heappop(q) * (-1)



print(ans)
import heapq  # 優先度付きキュー(最小値取り出し)

n, m = list(map(int, input().split()))
a = [list(map(int, input().split())) for i in range(n)]
a.sort(reverse=True)
b = []
heapq.heapify(b)  # ヒープ化
ans = 0
for i in range(1, m + 1):
    while a and a[-1][0] == i:
        val = a.pop()[1]
        heapq.heappush(b, (-1) * (val))  # 今考えてる日にちの価値をpush
    if b:
        val = heapq.heappop(b) * (-1)  # 最大値の取り出し
        ans += val
print(ans)

import heapq  # 優先度付きキュー(最小値取り出し)

inf = 10**15
mod = 10**9 + 7
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort(key=lambda x: x[0], reverse=True)
q = []
heapq.heapify(q)  # ヒープ化
ans = 0
for i in range(1, m + 1):  # m-i日目からスタートして間に合う仕事
    while ab and ab[-1][0] == i:
        _, val = ab.pop()
        heapq.heappush(q, (-1) * val)
    if q:
        ans += heapq.heappop(q) * (-1)
print(ans)

+inf = 10**15 +mod = 10**9 + 7 -a = [list(map(int, input().split())) for i in range(n)] -a.sort(reverse=True) -b = [] -heapq.heapify(b)  # ヒープ化 +ab = [list(map(int, input().split())) for i in range(n)] +ab.sort(key=lambda x: x[0], reverse=True) +q = [] +heapq.heapify(q)  # ヒープ化 -for i in range(1, m + 1): -    while a and a[-1][0] == i: -        val = a.pop()[1] -        heapq.heappush(b, (-1) * (val))  # 今考えてる日にちの価値をpush -    if b: -        val = heapq.heappop(b) * (-1)  # 最大値の取り出し -        ans += val +for i in range(1, m + 1):  # m-i日目からスタートして間に合う仕事 +    while ab and ab[-1][0] == i: +        _, val = ab.pop() +        heapq.heappush(q, (-1) * val) +    if q: +        ans += heapq.heappop(q) * (-1)