import heapq



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

arb = [[] for _ in range(M + 1)]



for _ in range(N) :

    a, b = list(map(int, input().split()))

    if a <= M :

        arb[a].append(-b)



arbList = []



ans = 0

for day in range(M + 1) :

    for b in arb[day] :

        heapq.heappush(arbList, b)

    if arbList :

        ans += heapq.heappop(arbList)



print((-ans))
from collections import defaultdict

import heapq



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



arbList = defaultdict(lambda: [])



for _ in range(N):

    a, b = list(map(int, input().split()))

    arbList[a].append(-b)



arb = []

ans = 0



for day in range(M + 1):

    for b in arbList[day]:

        heapq.heappush(arb, b)

    if arb:

        ans += heapq.heappop(arb)



print((-ans))
import heapq

N, M = list(map(int, input().split()))
arb = [[] for _ in range(M + 1)]
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a <= M:
        arb[a].append(-b)
arbList = []
ans = 0
for day in range(M + 1):
    for b in arb[day]:
        heapq.heappush(arbList, b)
    if arbList:
        ans += heapq.heappop(arbList)
print((-ans))

from collections import defaultdict
import heapq

N, M = list(map(int, input().split()))
arbList = defaultdict(lambda: [])
for _ in range(N):
    a, b = list(map(int, input().split()))
    arbList[a].append(-b)
arb = []
ans = 0
for day in range(M + 1):
    for b in arbList[day]:
        heapq.heappush(arb, b)
    if arb:
        ans += heapq.heappop(arb)
print((-ans))

+from collections import defaultdict -arb = [[] for _ in range(M + 1)] +arbList = defaultdict(lambda: []) -    if a <= M: -        arb[a].append(-b) -arbList = [] +    arbList[a].append(-b) +arb = [] -    for b in arb[day]: -        heapq.heappush(arbList, b) -    if arbList: -        ans += heapq.heappop(arbList) +    for b in arbList[day]: +        heapq.heappush(arb, b) +    if arb: +        ans += heapq.heappop(arb)