def main():

    from heapq import heapify,heappop,heappush



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

    W=[list(map(int,input().split())) for _ in range(n)]

    W.sort(key=lambda x:x[0])

    

    q=[]

    heapify(q)

    j,income=0,0

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

        while j<n and W[j][0]<=i:

            heappush(q,-W[j][1])

            j+=1



        if q:

            t=heappop(q)

            income+=-t

    print(income) 



if __name__=='__main__':

    main()
MOD = 10 ** 9 + 7

INF = 10 ** 11

import sys

sys.setrecursionlimit(100000000)

from heapq import heapify,heappop,heappush



def main():

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

    works = [tuple(map(int,input().split())) for _ in range(N)]

    works.sort(key = lambda x:x[0])

    q = []

    ans = 0

    j = 0

    for i in range(M):

        while j < N and works[j][0] <= i + 1:

            heappush(q,-works[j][1])

            j += 1

        if q:

            p = heappop(q)

            ans -= p

    print(ans)

if __name__ == '__main__':

    main()

def main():
    from heapq import heapify, heappop, heappush

    n, m = list(map(int, input().split()))
    W = [list(map(int, input().split())) for _ in range(n)]
    W.sort(key=lambda x: x[0])
    q = []
    heapify(q)
    j, income = 0, 0
    for i in range(1, m + 1):
        while j < n and W[j][0] <= i:
            heappush(q, -W[j][1])
            j += 1
        if q:
            t = heappop(q)
            income += -t
    print(income)


if __name__ == "__main__":
    main()

MOD = 10**9 + 7
INF = 10**11
import sys

sys.setrecursionlimit(100000000)
from heapq import heapify, heappop, heappush


def main():
    N, M = list(map(int, input().split()))
    works = [tuple(map(int, input().split())) for _ in range(N)]
    works.sort(key=lambda x: x[0])
    q = []
    ans = 0
    j = 0
    for i in range(M):
        while j < N and works[j][0] <= i + 1:
            heappush(q, -works[j][1])
            j += 1
        if q:
            p = heappop(q)
            ans -= p
    print(ans)


if __name__ == "__main__":
    main()

+MOD = 10**9 + 7 +INF = 10**11 +import sys + +sys.setrecursionlimit(100000000) +from heapq import heapify, heappop, heappush + + -    from heapq import heapify, heappop, heappush - -    n, m = list(map(int, input().split())) -    W = [list(map(int, input().split())) for _ in range(n)] -    W.sort(key=lambda x: x[0]) +    N, M = list(map(int, input().split())) +    works = [tuple(map(int, input().split())) for _ in range(N)] +    works.sort(key=lambda x: x[0]) -    heapify(q) -    j, income = 0, 0 -    for i in range(1, m + 1): -        while j < n and W[j][0] <= i: -            heappush(q, -W[j][1]) +    ans = 0 +    j = 0 +    for i in range(M): +        while j < N and works[j][0] <= i + 1: +            heappush(q, -works[j][1]) -            t = heappop(q) -            income += -t -    print(income) +            p = heappop(q) +            ans -= p +    print(ans)