from heapq import *



R = lambda: list(map(int, input().split()))

n, m = R()

c = [[] for _ in range(m)]

for _ in range(n):

    a, b = R()

    if a <= m:

        heappush(c[m - a], -b)

r=0

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

    if c[i]:

        r -= heappop(c[i])

        if i:

            f = len(c[i]) > len(c[i - 1])

            h, g = (c[i], c[i - 1]) if f else (c[i - 1], c[i])

            for x in g:

                heappush(h, x)

            if f:

                c[i - 1] = c[i]       

print(r)

from heapq import heappush, heappop



R = lambda: list(map(int, input().split()))

n, m = R()

c = [[] for _ in range(m)]

for _ in range(n):

    a, b = R()

    if a <= m:

        c[m - a].append(b)

r=0

h = []

for l in reversed(c):

    for x in l:

        heappush(h, -x)

    if h:

        r -= heappop(h) 

print(r)

from heapq import *

R = lambda: list(map(int, input().split()))
n, m = R()
c = [[] for _ in range(m)]
for _ in range(n):
    a, b = R()
    if a <= m:
        heappush(c[m - a], -b)
r = 0
for i in range(m - 1, -1, -1):
    if c[i]:
        r -= heappop(c[i])
        if i:
            f = len(c[i]) > len(c[i - 1])
            h, g = (c[i], c[i - 1]) if f else (c[i - 1], c[i])
            for x in g:
                heappush(h, x)
            if f:
                c[i - 1] = c[i]
print(r)

from heapq import heappush, heappop

R = lambda: list(map(int, input().split()))
n, m = R()
c = [[] for _ in range(m)]
for _ in range(n):
    a, b = R()
    if a <= m:
        c[m - a].append(b)
r = 0
h = []
for l in reversed(c):
    for x in l:
        heappush(h, -x)
    if h:
        r -= heappop(h)
print(r)

-from heapq import * +from heapq import heappush, heappop -        heappush(c[m - a], -b) +        c[m - a].append(b) -for i in range(m - 1, -1, -1): -    if c[i]: -        r -= heappop(c[i]) -        if i: -            f = len(c[i]) > len(c[i - 1]) -            h, g = (c[i], c[i - 1]) if f else (c[i - 1], c[i]) -            for x in g: -                heappush(h, x) -            if f: -                c[i - 1] = c[i] +h = [] +for l in reversed(c): +    for x in l: +        heappush(h, -x) +    if h: +        r -= heappop(h)