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 = 0, 1, m

    ans = 0

    while index < n:

        _a, _b = ab[index]

        if _a > m:

            break

        if _a <= cap:

            heapq.heappush(hq, -_b)

            index += 1

        else:

            if len(hq) > 0:

                ans += -heapq.heappop(hq)

            count -= 1

            cap += 1

    for _ in range(count):

        if len(hq) > 0:

            ans += -heapq.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) if len(hq) > 0 else 0 for _ in range(count)])

    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 = 0, 1, m
    ans = 0
    while index < n:
        _a, _b = ab[index]
        if _a > m:
            break
        if _a <= cap:
            heapq.heappush(hq, -_b)
            index += 1
        else:
            if len(hq) > 0:
                ans += -heapq.heappop(hq)
            count -= 1
            cap += 1
    for _ in range(count):
        if len(hq) > 0:
            ans += -heapq.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) if len(hq) > 0 else 0 for _ in range(count)])
    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)])))
    )

-    hq = [] -    index, cap, count = 0, 1, m -    ans = 0 +    hq, index, cap, count, ans = [], 0, 1, m, 0 -            if len(hq) > 0: -                ans += -heapq.heappop(hq) +            ans += -heapq.heappop(hq) if len(hq) > 0 else 0 -    for _ in range(count): -        if len(hq) > 0: -            ans += -heapq.heappop(hq) +    ans += sum([-heapq.heappop(hq) if len(hq) > 0 else 0 for _ in range(count)])