import heapq

class Heapq :

    def __init__(self, arr, desc=False) :

        if desc :

            arr = [-a for a in arr]

        self.sign = -1 if desc else 1

        self.hq = arr

        heapq.heapify(self.hq)

    def pop(self) :

        return heapq.heappop(self.hq) * self.sign

    def push(self, a):

        heapq.heappush(self.hq, a * self.sign)

    def top(self):

        return self.hq[0] * self.sign

    def size(self):

        return len(self.hq)



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

D = {i: [] for i in range(1,M+1)}

for i in range(N) :

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

    if a > M : continue

    D[a].append(b)



q = Heapq([], True)

ans = 0



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

    for b in D[a] :

        q.push(b)

    if q.size() > 0 :

        ans += q.pop()

print(ans)

import heapq

class Heapq :

    def __init__(self, arr, desc=False) :

        if desc :

            arr = [-a for a in arr]

        self.sign = -1 if desc else 1

        self.hq = arr

        heapq.heapify(self.hq)

    def pop(self) :

        return heapq.heappop(self.hq) * self.sign

    def push(self, a):

        heapq.heappush(self.hq, a * self.sign)

    def top(self):

        return self.hq[0] * self.sign

    def size(self):

        return len(self.hq)





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

AB = [list(map(int,input().split())) for i in range(N)]



L = [[] for i in range(10**5+1)]

for a,b in AB :

    L[a].append(b)



q = Heapq([], desc=True)

ans = 0

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

    for j in range(len(L[i])) :

        q.push(L[i][j])

    ans += q.pop() if q.size() > 0 else 0

print(ans)

import heapq


class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign

    def size(self):
        return len(self.hq)


N, M = list(map(int, input().split()))
D = {i: [] for i in range(1, M + 1)}
for i in range(N):
    a, b = list(map(int, input().split()))
    if a > M:
        continue
    D[a].append(b)
q = Heapq([], True)
ans = 0
for a in range(1, M + 1):
    for b in D[a]:
        q.push(b)
    if q.size() > 0:
        ans += q.pop()
print(ans)

import heapq


class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign

    def size(self):
        return len(self.hq)


N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(N)]
L = [[] for i in range(10**5 + 1)]
for a, b in AB:
    L[a].append(b)
q = Heapq([], desc=True)
ans = 0
for i in range(1, M + 1):
    for j in range(len(L[i])):
        q.push(L[i][j])
    ans += q.pop() if q.size() > 0 else 0
print(ans)

-D = {i: [] for i in range(1, M + 1)} -for i in range(N): -    a, b = list(map(int, input().split())) -    if a > M: -        continue -    D[a].append(b) -q = Heapq([], True) +AB = [list(map(int, input().split())) for i in range(N)] +L = [[] for i in range(10**5 + 1)] +for a, b in AB: +    L[a].append(b) +q = Heapq([], desc=True) -for a in range(1, M + 1): -    for b in D[a]: -        q.push(b) -    if q.size() > 0: -        ans += q.pop() +for i in range(1, M + 1): +    for j in range(len(L[i])): +        q.push(L[i][j]) +    ans += q.pop() if q.size() > 0 else 0