def slove():

    import sys

    import heapq

    input = sys.stdin.readline

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

    ls =[]

    v = []

    heapq.heapify(v)

    for i in range(n):

        a, b = list(map(int, input().rstrip('\n').split()))

        ls.append([a, b])

    ls.sort(reverse=True)

    r = 0

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

        while True:

            if len(ls) != 0:

                a, b = ls.pop()

                if a <= i:

                    heapq.heappush(v, -b)

                else:

                    ls.append([a, b])

                    break

            else:

                break

        if len(v) != 0:

            r += -heapq.heappop(v)

    print(r)





if __name__ == '__main__':

    slove()

import sys

import heapq





def solve():

    input = sys.stdin.readline

    mod = 10 ** 9 + 7

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

    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]

    ab.sort(reverse=True)

    ls = []

    heapq.heapify(ls)

    t = 0

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

        while True:

            if len(ab) != 0:

                if ab[-1][0] <= i:

                    heapq.heappush(ls, -ab.pop()[1])

                else:

                    break

            else:

                break

        if len(ls) != 0:

            t -= heapq.heappop(ls)

    print(t)





if __name__ == '__main__':

    solve()

def slove():
    import sys
    import heapq

    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip("\n").split()))
    ls = []
    v = []
    heapq.heapify(v)
    for i in range(n):
        a, b = list(map(int, input().rstrip("\n").split()))
        ls.append([a, b])
    ls.sort(reverse=True)
    r = 0
    for i in range(1, m + 1):
        while True:
            if len(ls) != 0:
                a, b = ls.pop()
                if a <= i:
                    heapq.heappush(v, -b)
                else:
                    ls.append([a, b])
                    break
            else:
                break
        if len(v) != 0:
            r += -heapq.heappop(v)
    print(r)


if __name__ == "__main__":
    slove()

import sys
import heapq


def solve():
    input = sys.stdin.readline
    mod = 10**9 + 7
    n, m = list(map(int, input().rstrip("\n").split()))
    ab = [list(map(int, input().rstrip("\n").split())) for _ in range(n)]
    ab.sort(reverse=True)
    ls = []
    heapq.heapify(ls)
    t = 0
    for i in range(1, m + 1):
        while True:
            if len(ab) != 0:
                if ab[-1][0] <= i:
                    heapq.heappush(ls, -ab.pop()[1])
                else:
                    break
            else:
                break
        if len(ls) != 0:
            t -= heapq.heappop(ls)
    print(t)


if __name__ == "__main__":
    solve()

-def slove(): -    import sys -    import heapq +import sys +import heapq + +def solve(): +    mod = 10**9 + 7 +    ab = [list(map(int, input().rstrip("\n").split())) for _ in range(n)] +    ab.sort(reverse=True) -    v = [] -    heapq.heapify(v) -    for i in range(n): -        a, b = list(map(int, input().rstrip("\n").split())) -        ls.append([a, b]) -    ls.sort(reverse=True) -    r = 0 +    heapq.heapify(ls) +    t = 0 -            if len(ls) != 0: -                a, b = ls.pop() -                if a <= i: -                    heapq.heappush(v, -b) +            if len(ab) != 0: +                if ab[-1][0] <= i: +                    heapq.heappush(ls, -ab.pop()[1]) -                    ls.append([a, b]) -        if len(v) != 0: -            r += -heapq.heappop(v) -    print(r) +        if len(ls) != 0: +            t -= heapq.heappop(ls) +    print(t) -    slove() +    solve()