import heapq

def main():

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

  jobs = [[] for _ in range(M)]

  for i in range(N):

    d, r = list(map(int, input().split()))

    if d-1 < M:

      jobs[d-1].append(-r)



  m = 0

  heap = []

  for i in range(M):

    for a in jobs[i]:

      heapq.heappush(heap, a)

    if len(heap) > 0:

      b = heapq.heappop(heap)

      m += -b

  print(m)

if __name__ == '__main__':

  main()

import sys

input = sys.stdin.readline

import heapq

def main():

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

  jobs = [[] for _ in range(M)]

  for i in range(N):

    d, r = list(map(int, input().split()))

    if d-1 < M:

      jobs[d-1].append(-r)



  m = 0

  heap = []

  for i in range(M):

    for a in jobs[i]:

      heapq.heappush(heap, a)

    if len(heap) > 0:

      b = heapq.heappop(heap)

      m += -b

  print(m)

if __name__ == '__main__':

  main()

import heapq


def main():
    N, M = list(map(int, input().split()))
    jobs = [[] for _ in range(M)]
    for i in range(N):
        d, r = list(map(int, input().split()))
        if d - 1 < M:
            jobs[d - 1].append(-r)
    m = 0
    heap = []
    for i in range(M):
        for a in jobs[i]:
            heapq.heappush(heap, a)
        if len(heap) > 0:
            b = heapq.heappop(heap)
            m += -b
    print(m)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline
import heapq


def main():
    N, M = list(map(int, input().split()))
    jobs = [[] for _ in range(M)]
    for i in range(N):
        d, r = list(map(int, input().split()))
        if d - 1 < M:
            jobs[d - 1].append(-r)
    m = 0
    heap = []
    for i in range(M):
        for a in jobs[i]:
            heapq.heappush(heap, a)
        if len(heap) > 0:
            b = heapq.heappop(heap)
            m += -b
    print(m)


if __name__ == "__main__":
    main()

+import sys + +input = sys.stdin.readline