def slove():

    import sys

    import heapq

    input = sys.stdin.readline

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

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

    r = []

    heapq.heapify(l)

    heapq.heapify(r)

    t = 0

    for i in range(m + 1):

        while True:

            if len(l) != 0:

                a, b = heapq.heappop(l)

                if a <= i:

                    heapq.heappush(r, -b)

                else:

                    heapq.heappush(l, [a, b])

                    break

            else:

                break

        if len(r) != 0:

            t += -heapq.heappop(r)

    print(t)





if __name__ == '__main__':

    slove()

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()

def slove():
    import sys
    import heapq

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


if __name__ == "__main__":
    slove()

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()

-    l = [list(map(int, input().rstrip("\n").split())) for _ in range(n)] -    r = [] -    heapq.heapify(l) -    heapq.heapify(r) -    t = 0 -    for i in range(m + 1): +    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): -            if len(l) != 0: -                a, b = heapq.heappop(l) +            if len(ls) != 0: +                a, b = ls.pop() -                    heapq.heappush(r, -b) +                    heapq.heappush(v, -b) -                    heapq.heappush(l, [a, b]) +                    ls.append([a, b]) -        if len(r) != 0: -            t += -heapq.heappop(r) -    print(t) +        if len(v) != 0: +            r += -heapq.heappop(v) +    print(r)