from heapq import*

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

q=[0]*7**6

v=[[]for _ in q]

z=0

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

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)]

q=[0]*7**6

v=[[]for _ in q]

z=0

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

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)]
q = [0] * 7**6
v = [[] for _ in q]
z = 0
for a, b in t:
    v[a - 1] += [b]
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)]
q = [0] * 7**6
v = [[] for _ in q]
z = 0
for a, b in t:
    v[a - 1] += [b]
for i in v[:m]:
    for j in i:
        heappush(q, -j)
    z -= heappop(q)
print(z)

-(N, M), *t = [list(map(int, s.split())) for s in open(0)] +(n, m), *t = [list(map(int, s.split())) for s in open(0)] -for i in v[:M]: +for i in v[:m]: