import sys
from heapq import heappop, heappush
N, M = map(int, sys.stdin.readline().split())
A = [[] for _ in [0] * M]
for line in sys.stdin:
    a, b = map(int, line.split())
    if a > M:
        continue
    A[M - a].append(b)
ans = 0
q = []
for i, B in enumerate(A):
    for b in B:
        heappush(q, b)
        ans += b
    while len(q) > i + 1:
        b = heappop(q)
        ans -= b
print(ans)

b78ebec09d29dc7b6b6ce2ef19504ee8c87eb982
<mask token>

<mask token>
for line in sys.stdin:
    a, b = map(int, line.split())
    if a > M:
        continue
    A[M - a].append(b)
<mask token>
for i, B in enumerate(A):
    for b in B:
        heappush(q, b)
        ans += b
    while len(q) > i + 1:
        b = heappop(q)
        ans -= b
print(ans)

<mask token>
N, M = map(int, sys.stdin.readline().split())
A = [[] for _ in [0] * M]
for line in sys.stdin:
    a, b = map(int, line.split())
    if a > M:
        continue
    A[M - a].append(b)
ans = 0
q = []
for i, B in enumerate(A):
    for b in B:
        heappush(q, b)
        ans += b
    while len(q) > i + 1:
        b = heappop(q)
        ans -= b
print(ans)

import sys
from heapq import heappop, heappush
N, M = map(int, sys.stdin.readline().split())
A = [[] for _ in [0] * M]
for line in sys.stdin:
    a, b = map(int, line.split())
    if a > M:
        continue
    A[M - a].append(b)
ans = 0
q = []
for i, B in enumerate(A):
    for b in B:
        heappush(q, b)
        ans += b
    while len(q) > i + 1:
        b = heappop(q)
        ans -= b
print(ans)
