import heapq



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



day_rew = {}

for _ in range(N):

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

    day_rew.setdefault(a,[])

    day_rew[a].append(b)



ans = 0

job_rew = []

heapq.heapify(job_rew)

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

    if i in day_rew:

        for r in day_rew[i]:

            heapq.heappush(job_rew, r*(-1))

    if job_rew:

        this_day_rew = heapq.heappop(job_rew)*(-1)

        ans += this_day_rew



print(ans)



import heapq



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

day_rews_l = [ [] for _ in range(m+1)]

for _ in range(n):

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

    if a > m: continue

    day_rews_l[a].append(b)





jobs = []

ans = 0

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

    for rew in day_rews_l[i]:

        heapq.heappush(jobs, (-1)*rew)

    if jobs:

        rew_i = heapq.heappop(jobs)

        ans += (-1)*rew_i



print(ans)

    
import heapq

N, M = list(map(int, input().split()))
day_rew = {}
for _ in range(N):
    a, b = list(map(int, input().split()))
    day_rew.setdefault(a, [])
    day_rew[a].append(b)
ans = 0
job_rew = []
heapq.heapify(job_rew)
for i in range(1, M + 1):
    if i in day_rew:
        for r in day_rew[i]:
            heapq.heappush(job_rew, r * (-1))
    if job_rew:
        this_day_rew = heapq.heappop(job_rew) * (-1)
        ans += this_day_rew
print(ans)

import heapq

n, m = list(map(int, input().split()))
day_rews_l = [[] for _ in range(m + 1)]
for _ in range(n):
    a, b = list(map(int, input().split()))
    if a > m:
        continue
    day_rews_l[a].append(b)
jobs = []
ans = 0
for i in range(1, m + 1):
    for rew in day_rews_l[i]:
        heapq.heappush(jobs, (-1) * rew)
    if jobs:
        rew_i = heapq.heappop(jobs)
        ans += (-1) * rew_i
print(ans)

-N, M = list(map(int, input().split())) -day_rew = {} -for _ in range(N): +n, m = list(map(int, input().split())) +day_rews_l = [[] for _ in range(m + 1)] +for _ in range(n): -    day_rew.setdefault(a, []) -    day_rew[a].append(b) +    if a > m: +        continue +    day_rews_l[a].append(b) +jobs = [] -job_rew = [] -heapq.heapify(job_rew) -for i in range(1, M + 1): -    if i in day_rew: -        for r in day_rew[i]: -            heapq.heappush(job_rew, r * (-1)) -    if job_rew: -        this_day_rew = heapq.heappop(job_rew) * (-1) -        ans += this_day_rew +for i in range(1, m + 1): +    for rew in day_rews_l[i]: +        heapq.heappush(jobs, (-1) * rew) +    if jobs: +        rew_i = heapq.heappop(jobs) +        ans += (-1) * rew_i