from heapq import*

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

z,*q=[0]*50*M

v=[[]for _ in q]

for a,b in t:v[a-1]+=b,

for i in v[:M]:[heappush(q,-j)for j in i];z-=heappop(q)

print(z)
from heapq import*

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

q=[0]*50*M

v=[[]for _ in q]

for a,b in t:v[a-1]+=b,

print((sum(([heappush(q,-j)for j in i],-heappop(q))[1]for i in v[:M])))
from heapq import *

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

from heapq import *

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

-z, *q = [0] * 50 * M +q = [0] * 50 * M -for i in v[:M]: -    [heappush(q, -j) for j in i] -    z -= heappop(q) -print(z) +print((sum(([heappush(q, -j) for j in i], -heappop(q))[1] for i in v[:M])))