import sys

import heapq



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

jobs = []

for line in sys.stdin:

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

    if A > M:

        continue

    jobs.append((A, B))



jobs.sort(reverse=True)



q = []

ans = 0

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

    while jobs and jobs[-1][0] <= i:

        A, B = jobs.pop()

        heapq.heappush(q, -B)



    if q:

        ans += -heapq.heappop(q)

print(ans)

import sys

import heapq



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

possiblyValidJobs = {}

for line in sys.stdin:

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

    if A > M:

        continue



    if A in possiblyValidJobs:

        possiblyValidJobs[A].append(B)

    else:

        possiblyValidJobs[A] = [B]



validJobsOnThisDay = []

ans = 0

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

    if daysBeforeDeadline in possiblyValidJobs:

        for wage in possiblyValidJobs[daysBeforeDeadline]:

            heapq.heappush(validJobsOnThisDay, -wage)

    if validJobsOnThisDay:

        ans += -heapq.heappop(validJobsOnThisDay)

print(ans)

import sys
import heapq

N, M = list(map(int, input().split()))
jobs = []
for line in sys.stdin:
    A, B = list(map(int, line.split()))
    if A > M:
        continue
    jobs.append((A, B))
jobs.sort(reverse=True)
q = []
ans = 0
for i in range(1, M + 1):
    while jobs and jobs[-1][0] <= i:
        A, B = jobs.pop()
        heapq.heappush(q, -B)
    if q:
        ans += -heapq.heappop(q)
print(ans)

import sys
import heapq

N, M = list(map(int, input().split()))
possiblyValidJobs = {}
for line in sys.stdin:
    A, B = list(map(int, line.split()))
    if A > M:
        continue
    if A in possiblyValidJobs:
        possiblyValidJobs[A].append(B)
    else:
        possiblyValidJobs[A] = [B]
validJobsOnThisDay = []
ans = 0
for daysBeforeDeadline in range(1, M + 1):
    if daysBeforeDeadline in possiblyValidJobs:
        for wage in possiblyValidJobs[daysBeforeDeadline]:
            heapq.heappush(validJobsOnThisDay, -wage)
    if validJobsOnThisDay:
        ans += -heapq.heappop(validJobsOnThisDay)
print(ans)

-jobs = [] +possiblyValidJobs = {} -    jobs.append((A, B)) -jobs.sort(reverse=True) -q = [] +    if A in possiblyValidJobs: +        possiblyValidJobs[A].append(B) +    else: +        possiblyValidJobs[A] = [B] +validJobsOnThisDay = [] -for i in range(1, M + 1): -    while jobs and jobs[-1][0] <= i: -        A, B = jobs.pop() -        heapq.heappush(q, -B) -    if q: -        ans += -heapq.heappop(q) +for daysBeforeDeadline in range(1, M + 1): +    if daysBeforeDeadline in possiblyValidJobs: +        for wage in possiblyValidJobs[daysBeforeDeadline]: +            heapq.heappush(validJobsOnThisDay, -wage) +    if validJobsOnThisDay: +        ans += -heapq.heappop(validJobsOnThisDay)