from bisect import bisect_left as bisect



def main():

    n, m, *ab = list(map(int, open(0).read().split()))



    rate = []

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

        if a > m:

            continue

        rate.append([a, b])



    rate = sorted(rate, key=lambda x: -x[1])



    day = list(range(1, m + 1))

    day_len = m

    ans = 0

    for term, money in rate:

        work_day = bisect(day, term)

        if work_day == day_len:

            continue

        else:

            del day[work_day]

            day_len -= 1

            ans += money



    print(ans)

    return()



if __name__ == '__main__':

    main()

import heapq



def main():

    n, m, *ab = list(map(int, open(0).read().split()))



    ab_dict = {}

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

        if a > m:

            continue

        if a in ab_dict:

            ab_dict[a].append(-b)

        else:

            ab_dict[a] = [-b]



    cand = []

    ans = 0

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

        if day in ab_dict:

            for cand_b in ab_dict[day]:

                heapq.heappush(cand, cand_b)

        if cand:

            ans += -heapq.heappop(cand)



    print(ans)

    return()



if __name__ == '__main__':

    main()

from bisect import bisect_left as bisect


def main():
    n, m, *ab = list(map(int, open(0).read().split()))
    rate = []
    for a, b in zip(*[iter(ab)] * 2):
        if a > m:
            continue
        rate.append([a, b])
    rate = sorted(rate, key=lambda x: -x[1])
    day = list(range(1, m + 1))
    day_len = m
    ans = 0
    for term, money in rate:
        work_day = bisect(day, term)
        if work_day == day_len:
            continue
        else:
            del day[work_day]
            day_len -= 1
            ans += money
    print(ans)
    return ()


if __name__ == "__main__":
    main()

import heapq


def main():
    n, m, *ab = list(map(int, open(0).read().split()))
    ab_dict = {}
    for a, b in zip(*[iter(ab)] * 2):
        if a > m:
            continue
        if a in ab_dict:
            ab_dict[a].append(-b)
        else:
            ab_dict[a] = [-b]
    cand = []
    ans = 0
    for day in range(1, m + 1):
        if day in ab_dict:
            for cand_b in ab_dict[day]:
                heapq.heappush(cand, cand_b)
        if cand:
            ans += -heapq.heappop(cand)
    print(ans)
    return ()


if __name__ == "__main__":
    main()

-from bisect import bisect_left as bisect +import heapq -    rate = [] +    ab_dict = {} -        rate.append([a, b]) -    rate = sorted(rate, key=lambda x: -x[1]) -    day = list(range(1, m + 1)) -    day_len = m +        if a in ab_dict: +            ab_dict[a].append(-b) +        else: +            ab_dict[a] = [-b] +    cand = [] -    for term, money in rate: -        work_day = bisect(day, term) -        if work_day == day_len: -            continue -        else: -            del day[work_day] -            day_len -= 1 -            ans += money +    for day in range(1, m + 1): +        if day in ab_dict: +            for cand_b in ab_dict[day]: +                heapq.heappush(cand, cand_b) +        if cand: +            ans += -heapq.heappop(cand)