import heapq

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

df = []

for _ in range(n):

    x, y = list(map(int, input().split(' ')))

    df.append([x, y])

df.sort()

h = []

result = 0

counter = 0

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

    while counter < len(df) and df[counter][0] <= i:

        heapq.heappush(h, (-1)*df[counter][1])

        counter += 1

    if len(h) == 0:

        continue

    else:

        result -= heapq.heappop(h)

print(result)
def main():

    import heapq

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

    df = []

    for _ in range(n):

        x, y = list(map(int, input().split(' ')))

        df.append([x, y])

    df.sort()

    h = []

    result = 0

    counter = 0

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

        while counter < len(df) and df[counter][0] <= i:

            heapq.heappush(h, (-1)*df[counter][1])

            counter += 1

        if len(h) == 0:

            continue

        else:

            result -= heapq.heappop(h)

    print(result)

if __name__ == '__main__':

    main()
import heapq

n, m = list(map(int, input().split(" ")))
df = []
for _ in range(n):
    x, y = list(map(int, input().split(" ")))
    df.append([x, y])
df.sort()
h = []
result = 0
counter = 0
for i in range(1, m + 1):
    while counter < len(df) and df[counter][0] <= i:
        heapq.heappush(h, (-1) * df[counter][1])
        counter += 1
    if len(h) == 0:
        continue
    else:
        result -= heapq.heappop(h)
print(result)

def main():
    import heapq

    n, m = list(map(int, input().split(" ")))
    df = []
    for _ in range(n):
        x, y = list(map(int, input().split(" ")))
        df.append([x, y])
    df.sort()
    h = []
    result = 0
    counter = 0
    for i in range(1, m + 1):
        while counter < len(df) and df[counter][0] <= i:
            heapq.heappush(h, (-1) * df[counter][1])
            counter += 1
        if len(h) == 0:
            continue
        else:
            result -= heapq.heappop(h)
    print(result)


if __name__ == "__main__":
    main()

-import heapq +def main(): +    import heapq -n, m = list(map(int, input().split(" "))) -df = [] -for _ in range(n): -    x, y = list(map(int, input().split(" "))) -    df.append([x, y]) -df.sort() -h = [] -result = 0 -counter = 0 -for i in range(1, m + 1): -    while counter < len(df) and df[counter][0] <= i: -        heapq.heappush(h, (-1) * df[counter][1]) -        counter += 1 -    if len(h) == 0: -        continue -    else: -        result -= heapq.heappop(h) -print(result) +    n, m = list(map(int, input().split(" "))) +    df = [] +    for _ in range(n): +        x, y = list(map(int, input().split(" "))) +        df.append([x, y]) +    df.sort() +    h = [] +    result = 0 +    counter = 0 +    for i in range(1, m + 1): +        while counter < len(df) and df[counter][0] <= i: +            heapq.heappush(h, (-1) * df[counter][1]) +            counter += 1 +        if len(h) == 0: +            continue +        else: +            result -= heapq.heappop(h) +    print(result) + + +if __name__ == "__main__": +    main()