def main():

    import heapq

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

    jobs = {}

    for i in range(n):

        a,b = list(map(int,input().split()))

        if a not in list(jobs.keys()):

            jobs[a] = [b]

        else:

            jobs[a] += [b]

    ob = []

    ans = 0

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

        if i in list(jobs.keys()):

            for j in range(len(jobs[i])):

                heapq.heappush(ob,-1*jobs[i][j])

        if len(ob)>0:

            ans += heapq.heappop(ob)*-1

    print(ans)



if __name__ == "__main__":

    main()
def main():

    import heapq

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

    job = {}

    for i in range(n):

        a,b = list(map(int,input().split()))

        if a not in list(job.keys()):

            job[a] = [b]

        else:

            job[a].append(b)

    ans = 0

    hp = []

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

        if i in list(job.keys()):

            for j in range(len(job[i])):

                heapq.heappush(hp,-1*job[i][j])

        if len(hp)>0:

            ans += -1*heapq.heappop(hp)

    print(ans)



if __name__ == "__main__":

    main()

def main():
    import heapq

    n, m = list(map(int, input().split()))
    jobs = {}
    for i in range(n):
        a, b = list(map(int, input().split()))
        if a not in list(jobs.keys()):
            jobs[a] = [b]
        else:
            jobs[a] += [b]
    ob = []
    ans = 0
    for i in range(1, m + 1):
        if i in list(jobs.keys()):
            for j in range(len(jobs[i])):
                heapq.heappush(ob, -1 * jobs[i][j])
        if len(ob) > 0:
            ans += heapq.heappop(ob) * -1
    print(ans)


if __name__ == "__main__":
    main()

def main():
    import heapq

    n, m = list(map(int, input().split()))
    job = {}
    for i in range(n):
        a, b = list(map(int, input().split()))
        if a not in list(job.keys()):
            job[a] = [b]
        else:
            job[a].append(b)
    ans = 0
    hp = []
    for i in range(1, m + 1):
        if i in list(job.keys()):
            for j in range(len(job[i])):
                heapq.heappush(hp, -1 * job[i][j])
        if len(hp) > 0:
            ans += -1 * heapq.heappop(hp)
    print(ans)


if __name__ == "__main__":
    main()

-    jobs = {} +    job = {} -        if a not in list(jobs.keys()): -            jobs[a] = [b] +        if a not in list(job.keys()): +            job[a] = [b] -            jobs[a] += [b] -    ob = [] +            job[a].append(b) +    hp = [] -        if i in list(jobs.keys()): -            for j in range(len(jobs[i])): -                heapq.heappush(ob, -1 * jobs[i][j]) -        if len(ob) > 0: -            ans += heapq.heappop(ob) * -1 +        if i in list(job.keys()): +            for j in range(len(job[i])): +                heapq.heappush(hp, -1 * job[i][j]) +        if len(hp) > 0: +            ans += -1 * heapq.heappop(hp)