import heapq



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



dic = {}

for i in range(n):

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

    if a >= m+1:

        continue

    if a in list(dic.keys()):

        dic[a].append(b)

    else:

        dic[a] = [b]



ans = 0

l = []

heapq.heapify(l)

for j in range(1, m+1):

    if j in list(dic.keys()):

        for x in dic[j]:

            heapq.heappush(l, -x)

    if l:

        ans += -l[0]

        heapq.heappop(l)



print(ans)
import heapq



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

memo = [[] for _ in range(m+1)]



for _ in range(n):

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

    if a <= m:

        memo[a].append(-b)



ans = 0

HQ = []

heapq.heapify(HQ)

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

    for j in memo[i]:

        heapq.heappush(HQ, j)

    if HQ:

        ans += heapq.heappop(HQ)



print((-ans))
import heapq

n, m = list(map(int, input().split()))
dic = {}
for i in range(n):
    a, b = list(map(int, input().split()))
    if a >= m + 1:
        continue
    if a in list(dic.keys()):
        dic[a].append(b)
    else:
        dic[a] = [b]
ans = 0
l = []
heapq.heapify(l)
for j in range(1, m + 1):
    if j in list(dic.keys()):
        for x in dic[j]:
            heapq.heappush(l, -x)
    if l:
        ans += -l[0]
        heapq.heappop(l)
print(ans)

import heapq

n, m = list(map(int, input().split()))
memo = [[] for _ in range(m + 1)]
for _ in range(n):
    a, b = list(map(int, input().split()))
    if a <= m:
        memo[a].append(-b)
ans = 0
HQ = []
heapq.heapify(HQ)
for i in range(1, m + 1):
    for j in memo[i]:
        heapq.heappush(HQ, j)
    if HQ:
        ans += heapq.heappop(HQ)
print((-ans))

-dic = {} -for i in range(n): +memo = [[] for _ in range(m + 1)] +for _ in range(n): -    if a >= m + 1: -        continue -    if a in list(dic.keys()): -        dic[a].append(b) -    else: -        dic[a] = [b] +    if a <= m: +        memo[a].append(-b) -l = [] -heapq.heapify(l) -for j in range(1, m + 1): -    if j in list(dic.keys()): -        for x in dic[j]: -            heapq.heappush(l, -x) -    if l: -        ans += -l[0] -        heapq.heappop(l) -print(ans) +HQ = [] +heapq.heapify(HQ) +for i in range(1, m + 1): +    for j in memo[i]: +        heapq.heappush(HQ, j) +    if HQ: +        ans += heapq.heappop(HQ) +print((-ans))