from heapq import heappop, heappush



N, M = list(map(int, input().split()))

W = []

for i in range(N):

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

  heappush(W, a)

  

ans = 0

C = []



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

  j = -i

  while W != [] and W[0][0] <= i:

    a,b = heappop(W)

    heappush(C,-b)

  if C != []:

    ans += -heappop(C)

print(ans)
from heapq import heappop, heappush

import sys

input = sys.stdin.readline



N, M = list(map(int, input().split()))

W = []

for i in range(N):

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

  heappush(W, a)

  

ans = 0

C = []



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

  j = -i

  while W != [] and W[0][0] <= i:

    a,b = heappop(W)

    heappush(C,-b)

  if C != []:

    ans += -heappop(C)

print(ans)
from heapq import heappop, heappush

N, M = list(map(int, input().split()))
W = []
for i in range(N):
    a = list(map(int, input().split()))
    heappush(W, a)
ans = 0
C = []
for i in range(1, M + 1):
    j = -i
    while W != [] and W[0][0] <= i:
        a, b = heappop(W)
        heappush(C, -b)
    if C != []:
        ans += -heappop(C)
print(ans)

from heapq import heappop, heappush
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
W = []
for i in range(N):
    a = list(map(int, input().split()))
    heappush(W, a)
ans = 0
C = []
for i in range(1, M + 1):
    j = -i
    while W != [] and W[0][0] <= i:
        a, b = heappop(W)
        heappush(C, -b)
    if C != []:
        ans += -heappop(C)
print(ans)

+import sys +input = sys.stdin.readline