import heapq



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

AB = {}

q = []

ans = 0



for i in range(N):

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

    

    if A in AB:

        AB[A].append(-B)

    else:

        AB[A] = [-B]



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

    if i in AB:

        for B in AB[i]:

            heapq.heappush(q, B)

        

    if len(q):

        ans -= heapq.heappop(q)

    

print(ans)    
import sys

from heapq import heappush, heappop



input = sys.stdin.readline



def main():

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

    A, B = [[] for i in range(M)], []

    

    for i in range(N):

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

        if a > M: continue

    

        A[a-1].append(-b)

        

    ans = 0

    

    for i in range(M):

        for a in A[i]: heappush(B, a)

        

        if not B: continue

    

        ans += heappop(B)

        

    print((-ans))

    

if __name__ == '__main__':

    main()

import heapq

N, M = list(map(int, input().split()))
AB = {}
q = []
ans = 0
for i in range(N):
    A, B = list(map(int, input().split()))
    if A in AB:
        AB[A].append(-B)
    else:
        AB[A] = [-B]
for i in range(1, M + 1):
    if i in AB:
        for B in AB[i]:
            heapq.heappush(q, B)
    if len(q):
        ans -= heapq.heappop(q)
print(ans)

import sys
from heapq import heappush, heappop

input = sys.stdin.readline


def main():
    N, M = list(map(int, input().split()))
    A, B = [[] for i in range(M)], []
    for i in range(N):
        a, b = list(map(int, input().split()))
        if a > M:
            continue
        A[a - 1].append(-b)
    ans = 0
    for i in range(M):
        for a in A[i]:
            heappush(B, a)
        if not B:
            continue
        ans += heappop(B)
    print((-ans))


if __name__ == "__main__":
    main()

-import heapq +import sys +from heapq import heappush, heappop -N, M = list(map(int, input().split())) -AB = {} -q = [] -ans = 0 -for i in range(N): -    A, B = list(map(int, input().split())) -    if A in AB: -        AB[A].append(-B) -    else: -        AB[A] = [-B] -for i in range(1, M + 1): -    if i in AB: -        for B in AB[i]: -            heapq.heappush(q, B) -    if len(q): -        ans -= heapq.heappop(q) -print(ans) +input = sys.stdin.readline + + +def main(): +    N, M = list(map(int, input().split())) +    A, B = [[] for i in range(M)], [] +    for i in range(N): +        a, b = list(map(int, input().split())) +        if a > M: +            continue +        A[a - 1].append(-b) +    ans = 0 +    for i in range(M): +        for a in A[i]: +            heappush(B, a) +        if not B: +            continue +        ans += heappop(B) +    print((-ans)) + + +if __name__ == "__main__": +    main()