import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

input=lambda:sys.stdin.readline().rstrip()

from heapq import heappop,heappush

def resolve():

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

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

    for _ in range(n):

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

        if(a>m): continue

        heappush(V[a-1],-b)





    ans=0

    Q=[]

    for d in range(m):

        if(V[d]):

            heappush(Q,(heappop(V[d]),d))

        if(Q):

            b,a=heappop(Q)

            ans+=-b

            if(V[a]):

                heappush(Q,(heappop(V[a]),a))



    print(ans)

resolve()
import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

input=lambda:sys.stdin.readline().rstrip()

from heapq import heappop,heappush

def resolve():

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

    AB=[None]*n

    for i in range(n):

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

        a-=1

        AB[i]=(a,b)

    AB.sort(reverse=1)



    Q=[]

    ans=0

    for d in range(m):

        while(AB and AB[-1][0]<=d):

            a,b=AB.pop()

            heappush(Q,-b)

        if(Q):

            ans+=heappop(Q)



    print((-ans))

resolve()
import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()
from heapq import heappop, heappush


def resolve():
    n, m = list(map(int, input().split()))
    V = [[] for _ in range(m)]
    for _ in range(n):
        a, b = list(map(int, input().split()))
        if a > m:
            continue
        heappush(V[a - 1], -b)
    ans = 0
    Q = []
    for d in range(m):
        if V[d]:
            heappush(Q, (heappop(V[d]), d))
        if Q:
            b, a = heappop(Q)
            ans += -b
            if V[a]:
                heappush(Q, (heappop(V[a]), a))
    print(ans)


resolve()

import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()
from heapq import heappop, heappush


def resolve():
    n, m = list(map(int, input().split()))
    AB = [None] * n
    for i in range(n):
        a, b = list(map(int, input().split()))
        a -= 1
        AB[i] = (a, b)
    AB.sort(reverse=1)
    Q = []
    ans = 0
    for d in range(m):
        while AB and AB[-1][0] <= d:
            a, b = AB.pop()
            heappush(Q, -b)
        if Q:
            ans += heappop(Q)
    print((-ans))


resolve()

-    V = [[] for _ in range(m)] -    for _ in range(n): +    AB = [None] * n +    for i in range(n): -        if a > m: -            continue -        heappush(V[a - 1], -b) +        a -= 1 +        AB[i] = (a, b) +    AB.sort(reverse=1) +    Q = [] -    Q = [] -        if V[d]: -            heappush(Q, (heappop(V[d]), d)) +        while AB and AB[-1][0] <= d: +            a, b = AB.pop() +            heappush(Q, -b) -            b, a = heappop(Q) -            ans += -b -            if V[a]: -                heappush(Q, (heappop(V[a]), a)) -    print(ans) +            ans += heappop(Q) +    print((-ans))