import heapq





def solve(string):

    n, m, *ab = list(map(int, string.split()))

    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)])

    hq, index, cap, count, ans = [], 0, 1, m, 0

    while index < n:

        _a, _b = ab[index]

        if _a > m:

            break

        if _a <= cap:

            heapq.heappush(hq, -_b)

            index += 1

        else:

            ans += -heapq.heappop(hq) if len(hq) > 0 else 0

            count += -1

            cap += 1

    ans += sum([-heapq.heappop(hq) for _ in range(count) if len(hq) > 0])

    return str(ans)





if __name__ == '__main__':

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

    print((solve('{} {}\n'.format(n, m) + '\n'.join([eval(input()) for _ in range(n)]))))

from heapq import heappop, heappush

from collections import defaultdict





def solve(string):

    n, m, *ab = list(map(int, string.split()))

    dd = defaultdict(list)

    for a, b in zip(*[iter(ab)] * 2):

        dd[a].append(-b)

    hq, ans = [], 0

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

        if d in dd:

            for _b in dd[d]:

                heappush(hq, _b)

        if hq:

            ans += heappop(hq)

    return str(-ans)





if __name__ == '__main__':

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

    print((solve('{} {}\n'.format(n, m) + '\n'.join([eval(input()) for _ in range(n)]))))

import heapq


def solve(string):
    n, m, *ab = list(map(int, string.split()))
    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)])
    hq, index, cap, count, ans = [], 0, 1, m, 0
    while index < n:
        _a, _b = ab[index]
        if _a > m:
            break
        if _a <= cap:
            heapq.heappush(hq, -_b)
            index += 1
        else:
            ans += -heapq.heappop(hq) if len(hq) > 0 else 0
            count += -1
            cap += 1
    ans += sum([-heapq.heappop(hq) for _ in range(count) if len(hq) > 0])
    return str(ans)


if __name__ == "__main__":
    n, m = list(map(int, input().split()))
    print(
        (solve("{} {}\n".format(n, m) + "\n".join([eval(input()) for _ in range(n)])))
    )

from heapq import heappop, heappush
from collections import defaultdict


def solve(string):
    n, m, *ab = list(map(int, string.split()))
    dd = defaultdict(list)
    for a, b in zip(*[iter(ab)] * 2):
        dd[a].append(-b)
    hq, ans = [], 0
    for d in range(1, m + 1):
        if d in dd:
            for _b in dd[d]:
                heappush(hq, _b)
        if hq:
            ans += heappop(hq)
    return str(-ans)


if __name__ == "__main__":
    n, m = list(map(int, input().split()))
    print(
        (solve("{} {}\n".format(n, m) + "\n".join([eval(input()) for _ in range(n)])))
    )

-import heapq +from heapq import heappop, heappush +from collections import defaultdict -    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)]) -    hq, index, cap, count, ans = [], 0, 1, m, 0 -    while index < n: -        _a, _b = ab[index] -        if _a > m: -            break -        if _a <= cap: -            heapq.heappush(hq, -_b) -            index += 1 -        else: -            ans += -heapq.heappop(hq) if len(hq) > 0 else 0 -            count += -1 -            cap += 1 -    ans += sum([-heapq.heappop(hq) for _ in range(count) if len(hq) > 0]) -    return str(ans) +    dd = defaultdict(list) +    for a, b in zip(*[iter(ab)] * 2): +        dd[a].append(-b) +    hq, ans = [], 0 +    for d in range(1, m + 1): +        if d in dd: +            for _b in dd[d]: +                heappush(hq, _b) +        if hq: +            ans += heappop(hq) +    return str(-ans)