from heapq import*

N,M,*t=list(map(int,open(0).read().split()))

v=[[]for _ in'_'*M]

for i in range(N):M<t[2*i]or v[t[2*i]-1].append(t[2*i+1])

z=0

q=[]

for i in v:

	for j in i:heappush(q,-j)

	z-=len(q)and heappop(q)

print(z)
from heapq import*

N,M,*t=list(map(int,open(0).read().split()))

v=[[]for _ in'_'*M]

z=i=0

for _ in'_'*N:M<t[i]or v[t[i]-1].append(t[i+1]);i+=2

q=[]

for i in v:

	for j in i:heappush(q,-j)

	z-=len(q)and heappop(q)

print(z)
from heapq import *

N, M, *t = list(map(int, open(0).read().split()))
v = [[] for _ in "_" * M]
for i in range(N):
    M < t[2 * i] or v[t[2 * i] - 1].append(t[2 * i + 1])
z = 0
q = []
for i in v:
    for j in i:
        heappush(q, -j)
    z -= len(q) and heappop(q)
print(z)

from heapq import *

N, M, *t = list(map(int, open(0).read().split()))
v = [[] for _ in "_" * M]
z = i = 0
for _ in "_" * N:
    M < t[i] or v[t[i] - 1].append(t[i + 1])
    i += 2
q = []
for i in v:
    for j in i:
        heappush(q, -j)
    z -= len(q) and heappop(q)
print(z)

-for i in range(N): -    M < t[2 * i] or v[t[2 * i] - 1].append(t[2 * i + 1]) -z = 0 +z = i = 0 +for _ in "_" * N: +    M < t[i] or v[t[i] - 1].append(t[i + 1]) +    i += 2