import sys

import heapq



s = sys.stdin.readlines()

n,m = list(map(int,s[0].split()))

a = []

b = []

l = []

ans = 0

count = 0



for i in range(1,n+1):

	a_dash,b_dash = list(map(int,s[i].split()))

	a.append(-a_dash)

	b.append(-b_dash)



c = list(zip(a,b))

c = sorted(c,reverse=True)

a,b = list(zip(*c))

a_length = len(a)



for i in reversed(list(range(m))):

	for j in range(count,a_length):

		if -a[j] + i == m:

			heapq.heappush(l,b[j])

			count += 1

		else:

			break

	if len(l) != 0:

		maximum = heapq.heappop(l)

		ans += maximum



print((-ans))

n,m = list(map(int,input().split()))

ab = [[] for _ in range(10**5)]

for i in range(n):

	a,b = list(map(int,input().split()))

	a -= 1

	ab[a].append(b)



import heapq



q = []

heapq.heapify(q)



ans = 0

for i in range(m):

	if len(ab[i]) != 0:

		for j in range(len(ab[i])):

			heapq.heappush(q,-1*ab[i][j])

	if len(q) != 0:

		ans += -1 * heapq.heappop(q)



print(ans)

import sys
import heapq

s = sys.stdin.readlines()
n, m = list(map(int, s[0].split()))
a = []
b = []
l = []
ans = 0
count = 0
for i in range(1, n + 1):
    a_dash, b_dash = list(map(int, s[i].split()))
    a.append(-a_dash)
    b.append(-b_dash)
c = list(zip(a, b))
c = sorted(c, reverse=True)
a, b = list(zip(*c))
a_length = len(a)
for i in reversed(list(range(m))):
    for j in range(count, a_length):
        if -a[j] + i == m:
            heapq.heappush(l, b[j])
            count += 1
        else:
            break
    if len(l) != 0:
        maximum = heapq.heappop(l)
        ans += maximum
print((-ans))

n, m = list(map(int, input().split()))
ab = [[] for _ in range(10**5)]
for i in range(n):
    a, b = list(map(int, input().split()))
    a -= 1
    ab[a].append(b)
import heapq

q = []
heapq.heapify(q)
ans = 0
for i in range(m):
    if len(ab[i]) != 0:
        for j in range(len(ab[i])):
            heapq.heappush(q, -1 * ab[i][j])
    if len(q) != 0:
        ans += -1 * heapq.heappop(q)
print(ans)

-import sys +n, m = list(map(int, input().split())) +ab = [[] for _ in range(10**5)] +for i in range(n): +    a, b = list(map(int, input().split())) +    a -= 1 +    ab[a].append(b) -s = sys.stdin.readlines() -n, m = list(map(int, s[0].split())) -a = [] -b = [] -l = [] +q = [] +heapq.heapify(q) -count = 0 -for i in range(1, n + 1): -    a_dash, b_dash = list(map(int, s[i].split())) -    a.append(-a_dash) -    b.append(-b_dash) -c = list(zip(a, b)) -c = sorted(c, reverse=True) -a, b = list(zip(*c)) -a_length = len(a) -for i in reversed(list(range(m))): -    for j in range(count, a_length): -        if -a[j] + i == m: -            heapq.heappush(l, b[j]) -            count += 1 -        else: -            break -    if len(l) != 0: -        maximum = heapq.heappop(l) -        ans += maximum -print((-ans)) +for i in range(m): +    if len(ab[i]) != 0: +        for j in range(len(ab[i])): +            heapq.heappush(q, -1 * ab[i][j]) +    if len(q) != 0: +        ans += -1 * heapq.heappop(q) +print(ans)