from heapq import *

from collections import defaultdict



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

jobs = defaultdict(list)

for _ in range(n):

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

    jobs[a].append(-b)



result = 0

pool = []

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

    for j in jobs[i]:

        heappush(pool, j)

    if 0 < len(pool):

        result += -heappop(pool)



print(result)
import sys

from collections import defaultdict

from heapq import *

sys.setrecursionlimit(10**8)



input = sys.stdin.readline



def main():

    """ main """

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

    job_list = defaultdict(list)

    for _ in range(N):

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

        job_list[a].append(-b)



    pool = []

    ans = 0

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

        for reward in job_list[i]:

            heappush(pool, reward)

        if 0 < len(pool):

            ans -= heappop(pool)

    print(ans)



if __name__ == '__main__':

    main()
from heapq import *
from collections import defaultdict

n, m = list(map(int, input().split()))
jobs = defaultdict(list)
for _ in range(n):
    a, b = list(map(int, input().split()))
    jobs[a].append(-b)
result = 0
pool = []
for i in range(1, m + 1):
    for j in jobs[i]:
        heappush(pool, j)
    if 0 < len(pool):
        result += -heappop(pool)
print(result)

import sys
from collections import defaultdict
from heapq import *

sys.setrecursionlimit(10**8)
input = sys.stdin.readline


def main():
    """main"""
    N, M = list(map(int, input().split()))
    job_list = defaultdict(list)
    for _ in range(N):
        a, b = list(map(int, input().split()))
        job_list[a].append(-b)
    pool = []
    ans = 0
    for i in range(1, M + 1):
        for reward in job_list[i]:
            heappush(pool, reward)
        if 0 < len(pool):
            ans -= heappop(pool)
    print(ans)


if __name__ == "__main__":
    main()

+import sys +from collections import defaultdict -from collections import defaultdict -n, m = list(map(int, input().split())) -jobs = defaultdict(list) -for _ in range(n): -    a, b = list(map(int, input().split())) -    jobs[a].append(-b) -result = 0 -pool = [] -for i in range(1, m + 1): -    for j in jobs[i]: -        heappush(pool, j) -    if 0 < len(pool): -        result += -heappop(pool) -print(result) +sys.setrecursionlimit(10**8) +input = sys.stdin.readline + + +def main(): +    """main""" +    N, M = list(map(int, input().split())) +    job_list = defaultdict(list) +    for _ in range(N): +        a, b = list(map(int, input().split())) +        job_list[a].append(-b) +    pool = [] +    ans = 0 +    for i in range(1, M + 1): +        for reward in job_list[i]: +            heappush(pool, reward) +        if 0 < len(pool): +            ans -= heappop(pool) +    print(ans) + + +if __name__ == "__main__": +    main()