import heapq



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

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



AB.sort()

#print("AB:", AB)



H = []

iab = 0

answer = 0

for i in range(M + 1):

  while iab <= N - 1 and AB[iab][0] <= i:

    heapq.heappush(H, -AB[iab][1])

    iab += 1

  if len(H):

    answer += heapq.heappop(H)

print((-answer))
import sys

input = sys.stdin.buffer.readline

from heapq import heappush, heappop



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

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



AB.sort()



h = []

index = 0

answer = 0

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

  while index < N:

    a, b = AB[index]

    if a == i:

      heappush(h, -b)

      index += 1

    else:

      break

  if h:

    answer += - heappop(h)

    

print(answer)
import heapq

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
# print("AB:", AB)
H = []
iab = 0
answer = 0
for i in range(M + 1):
    while iab <= N - 1 and AB[iab][0] <= i:
        heapq.heappush(H, -AB[iab][1])
        iab += 1
    if len(H):
        answer += heapq.heappop(H)
print((-answer))

import sys

input = sys.stdin.buffer.readline
from heapq import heappush, heappop

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
h = []
index = 0
answer = 0
for i in range(1, M + 1):
    while index < N:
        a, b = AB[index]
        if a == i:
            heappush(h, -b)
            index += 1
        else:
            break
    if h:
        answer += -heappop(h)
print(answer)

-import heapq +import sys + +input = sys.stdin.buffer.readline +from heapq import heappush, heappop -# print("AB:", AB) -H = [] -iab = 0 +h = [] +index = 0 -for i in range(M + 1): -    while iab <= N - 1 and AB[iab][0] <= i: -        heapq.heappush(H, -AB[iab][1]) -        iab += 1 -    if len(H): -        answer += heapq.heappop(H) -print((-answer)) +for i in range(1, M + 1): +    while index < N: +        a, b = AB[index] +        if a == i: +            heappush(h, -b) +            index += 1 +        else: +            break +    if h: +        answer += -heappop(h) +print(answer)