import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)



from heapq import heappush, heappushpop



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

tasks = [[] for _ in range(M+1)] # 価値、締切

for _ in range(N):

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

    if a <= M:

        tasks[M-a].append(b)



q = []

for day in range(M+1):

    for v in tasks[day]:

        if len(q) < day+1:

            heappush(q,v)

        else:

            heappushpop(q,v)



answer = sum(q)

print(answer)
import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



from heapq import heappush, heappop



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

m = list(map(int,read().split()))

AB = [[] for _ in range(M+1)] # A に対する B

for a,b in zip(m,m):

    if a <= M:

        AB[a].append(b)



"""

・逆再生。最終日から順に、扱える仕事が増えていく。

"""



q = []

answer = 0

for B in AB:

    for b in B:

        heappush(q,-b)

    if q:

        answer -= heappop(q)



print(answer)
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from heapq import heappush, heappushpop

N, M = list(map(int, input().split()))
tasks = [[] for _ in range(M + 1)]  # 価値、締切
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a <= M:
        tasks[M - a].append(b)
q = []
for day in range(M + 1):
    for v in tasks[day]:
        if len(q) < day + 1:
            heappush(q, v)
        else:
            heappushpop(q, v)
answer = sum(q)
print(answer)

import sys

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

N, M = list(map(int, readline().split()))
m = list(map(int, read().split()))
AB = [[] for _ in range(M + 1)]  # A に対する B
for a, b in zip(m, m):
    if a <= M:
        AB[a].append(b)
"""
・逆再生。最終日から順に、扱える仕事が増えていく。
"""
q = []
answer = 0
for B in AB:
    for b in B:
        heappush(q, -b)
    if q:
        answer -= heappop(q)
print(answer)

-input = sys.stdin.readline -sys.setrecursionlimit(10**7) -from heapq import heappush, heappushpop +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines +from heapq import heappush, heappop -N, M = list(map(int, input().split())) -tasks = [[] for _ in range(M + 1)]  # 価値、締切 -for _ in range(N): -    a, b = list(map(int, input().split())) +N, M = list(map(int, readline().split())) +m = list(map(int, read().split())) +AB = [[] for _ in range(M + 1)]  # A に対する B +for a, b in zip(m, m): -        tasks[M - a].append(b) +        AB[a].append(b) +""" +・逆再生。最終日から順に、扱える仕事が増えていく。 +""" -for day in range(M + 1): -    for v in tasks[day]: -        if len(q) < day + 1: -            heappush(q, v) -        else: -            heappushpop(q, v) -answer = sum(q) +answer = 0 +for B in AB: +    for b in B: +        heappush(q, -b) +    if q: +        answer -= heappop(q)