from heapq import*

(N, M), *t = [list(map(int, s.split())) for s in open(0)]

q = [0] * 10**5

v = [[] for _ in q]

for a, b in t:

    v[a - 1] += b,

z = 0

for i in v[:M]:

    for j in i:

        heappush(q, -j)

    z += -heappop(q)

print(z)
from heapq import*

(N, M), *t = [list(map(int, s.split())) for s in open(0)]

v = [[] for _ in [None] * 10**5]

for a, b in t:

    v[a - 1] += b,

z = 0

q = []

for i in v[:M]:

    for j in i:

        heappush(q, -j)

    if q:

        z += -heappop(q)

print(z)
from heapq import *

(N, M), *t = [list(map(int, s.split())) for s in open(0)]
q = [0] * 10**5
v = [[] for _ in q]
for a, b in t:
    v[a - 1] += (b,)
z = 0
for i in v[:M]:
    for j in i:
        heappush(q, -j)
    z += -heappop(q)
print(z)

from heapq import *

(N, M), *t = [list(map(int, s.split())) for s in open(0)]
v = [[] for _ in [None] * 10**5]
for a, b in t:
    v[a - 1] += (b,)
z = 0
q = []
for i in v[:M]:
    for j in i:
        heappush(q, -j)
    if q:
        z += -heappop(q)
print(z)

-q = [0] * 10**5 -v = [[] for _ in q] +v = [[] for _ in [None] * 10**5] +q = [] -    z += -heappop(q) +    if q: +        z += -heappop(q)