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

tasks=[None]*n

for i in range(n):

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

  tasks[i]=[a,b]



tasks=sorted(tasks,key=lambda x:x[0])

import heapq

que=[]

ans=0

heapq.heapify(que)

j=0

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

  left=m-i

  while j<len(tasks):

    if tasks[j][0]<=left:

      heapq.heappush(que, tasks[j][1]*(-1))

    else:

      break

    j+=1

  if len(que)>0:

    val=heapq.heappop(que)*(-1)

    ans+=val

      

print(ans)

import sys

readline = sys.stdin.readline



# 逆順に見ていく

# A_iの値が小さい順にheapqに追加していく



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

from collections import defaultdict

tasks = defaultdict(list)

for i in range(N):

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

  tasks[a].append(b)

  

import heapq

q = []

heapq.heapify(q)

ans = 0

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

  for task in tasks[i]:

    heapq.heappush(q,-task)

  if q:

    t = heapq.heappop(q)

    ans += (-t)



print(ans)
n, m = list(map(int, input().split()))
tasks = [None] * n
for i in range(n):
    a, b = list(map(int, input().split()))
    tasks[i] = [a, b]
tasks = sorted(tasks, key=lambda x: x[0])
import heapq

que = []
ans = 0
heapq.heapify(que)
j = 0
for i in range(m, -1, -1):
    left = m - i
    while j < len(tasks):
        if tasks[j][0] <= left:
            heapq.heappush(que, tasks[j][1] * (-1))
        else:
            break
        j += 1
    if len(que) > 0:
        val = heapq.heappop(que) * (-1)
        ans += val
print(ans)

import sys

readline = sys.stdin.readline
# 逆順に見ていく
# A_iの値が小さい順にheapqに追加していく
N, M = list(map(int, readline().split()))
from collections import defaultdict

tasks = defaultdict(list)
for i in range(N):
    a, b = list(map(int, readline().split()))
    tasks[a].append(b)
import heapq

q = []
heapq.heapify(q)
ans = 0
for i in range(1, M + 1):
    for task in tasks[i]:
        heapq.heappush(q, -task)
    if q:
        t = heapq.heappop(q)
        ans += -t
print(ans)

-n, m = list(map(int, input().split())) -tasks = [None] * n -for i in range(n): -    a, b = list(map(int, input().split())) -    tasks[i] = [a, b] -tasks = sorted(tasks, key=lambda x: x[0]) +import sys + +readline = sys.stdin.readline +# 逆順に見ていく +# A_iの値が小さい順にheapqに追加していく +N, M = list(map(int, readline().split())) +from collections import defaultdict + +tasks = defaultdict(list) +for i in range(N): +    a, b = list(map(int, readline().split())) +    tasks[a].append(b) -que = [] +q = [] +heapq.heapify(q) -heapq.heapify(que) -j = 0 -for i in range(m, -1, -1): -    left = m - i -    while j < len(tasks): -        if tasks[j][0] <= left: -            heapq.heappush(que, tasks[j][1] * (-1)) -        else: -            break -        j += 1 -    if len(que) > 0: -        val = heapq.heappop(que) * (-1) -        ans += val +for i in range(1, M + 1): +    for task in tasks[i]: +        heapq.heappush(q, -task) +    if q: +        t = heapq.heappop(q) +        ans += -t