import heapq

from collections import deque



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

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

AB.sort(key=lambda x:x[0])

d=deque(AB)

h=[]

ans=0

for i in range(M+1):

    while d and d[0][0]<=i:

        heapq.heappush(h, -d.popleft()[1])

    if h:

        ans-=heapq.heappop(h)

print(ans)
import heapq

from collections import defaultdict



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

d = defaultdict(list)

for i in range(N):

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

    d[A].append(-B)



ans = 0

work = []

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

    for j in range(len(d[i])):

        heapq.heappush(work, d[i][j])

    if len(work):

        ans -= heapq.heappop(work)

print(ans)
import heapq
from collections import deque

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[0])
d = deque(AB)
h = []
ans = 0
for i in range(M + 1):
    while d and d[0][0] <= i:
        heapq.heappush(h, -d.popleft()[1])
    if h:
        ans -= heapq.heappop(h)
print(ans)

import heapq
from collections import defaultdict

N, M = list(map(int, input().split()))
d = defaultdict(list)
for i in range(N):
    A, B = list(map(int, input().split()))
    d[A].append(-B)
ans = 0
work = []
for i in range(1, M + 1):
    for j in range(len(d[i])):
        heapq.heappush(work, d[i][j])
    if len(work):
        ans -= heapq.heappop(work)
print(ans)

-from collections import deque +from collections import defaultdict -AB = [list(map(int, input().split())) for _ in range(N)] -AB.sort(key=lambda x: x[0]) -d = deque(AB) -h = [] +d = defaultdict(list) +for i in range(N): +    A, B = list(map(int, input().split())) +    d[A].append(-B) -for i in range(M + 1): -    while d and d[0][0] <= i: -        heapq.heappush(h, -d.popleft()[1]) -    if h: -        ans -= heapq.heappop(h) +work = [] +for i in range(1, M + 1): +    for j in range(len(d[i])): +        heapq.heappush(work, d[i][j]) +    if len(work): +        ans -= heapq.heappop(work)