from heapq import heappop, heappush

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

AB = [(a, b) for a, b in zip(AB[::2], AB[1::2]) if a <= m]

AB_ = [[] for _ in range(m)]

for a, b in AB:

    AB_[a-1].append(b)



ans = 0

heap = []

for ab in AB_:

    for x in ab:

        heappush(heap, -x)

    if heap:

        ans -= heappop(heap)

print(ans)

from heapq import heappop, heappush

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

B = [[] for _ in range(m)]

for a, b in zip(AB[::2], AB[1::2]):

    if a <= m:

        B[a-1].append(b)



ans = 0

heap = []

for b in B:

    for x in b:

        heappush(heap, -x)

    if heap:

        ans -= heappop(heap)

print(ans)

from heapq import heappop, heappush

n, m, *AB = list(map(int, open(0).read().split()))
AB = [(a, b) for a, b in zip(AB[::2], AB[1::2]) if a <= m]
AB_ = [[] for _ in range(m)]
for a, b in AB:
    AB_[a - 1].append(b)
ans = 0
heap = []
for ab in AB_:
    for x in ab:
        heappush(heap, -x)
    if heap:
        ans -= heappop(heap)
print(ans)

from heapq import heappop, heappush

n, m, *AB = list(map(int, open(0).read().split()))
B = [[] for _ in range(m)]
for a, b in zip(AB[::2], AB[1::2]):
    if a <= m:
        B[a - 1].append(b)
ans = 0
heap = []
for b in B:
    for x in b:
        heappush(heap, -x)
    if heap:
        ans -= heappop(heap)
print(ans)

-AB = [(a, b) for a, b in zip(AB[::2], AB[1::2]) if a <= m] -AB_ = [[] for _ in range(m)] -for a, b in AB: -    AB_[a - 1].append(b) +B = [[] for _ in range(m)] +for a, b in zip(AB[::2], AB[1::2]): +    if a <= m: +        B[a - 1].append(b) -for ab in AB_: -    for x in ab: +for b in B: +    for x in b: