def main():

    import heapq

    n, m, *ab = list(map(int, open(0).read().split()))

    l = [[] for _ in range(m + 1)]

    for i, j in zip(*[iter(ab)] * 2):

        if i <= m:

            heapq.heappush(l[i], -j)

    

    ans = 0

    h = []

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

        for j in l[i]:

            heapq.heappush(h, j)

        if len(h) > 0:

            x = heapq.heappop(h)

            ans += x



    print((-ans))





if __name__ == '__main__':

    main()

def main():

    from heapq import heappush as sushi

    from heapq import heappop as tempra

    n, m, *ab = list(map(int, open(0).read().split()))

    l = [[] for _ in range(m + 1)]

    for i, j in zip(*[iter(ab)] * 2):

        if i <= m:

            l[i].append(-j)



    w = []

    h = []

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

        for j in l[i]:

            sushi(h, j)

        if len(h) > 0:

            x = tempra(h)

            w.append(x)



    print((-sum(w)))





if __name__ == '__main__':

    main()

def main():
    import heapq

    n, m, *ab = list(map(int, open(0).read().split()))
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i <= m:
            heapq.heappush(l[i], -j)
    ans = 0
    h = []
    for i in range(1, m + 1):
        for j in l[i]:
            heapq.heappush(h, j)
        if len(h) > 0:
            x = heapq.heappop(h)
            ans += x
    print((-ans))


if __name__ == "__main__":
    main()

def main():
    from heapq import heappush as sushi
    from heapq import heappop as tempra

    n, m, *ab = list(map(int, open(0).read().split()))
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i <= m:
            l[i].append(-j)
    w = []
    h = []
    for i in range(1, m + 1):
        for j in l[i]:
            sushi(h, j)
        if len(h) > 0:
            x = tempra(h)
            w.append(x)
    print((-sum(w)))


if __name__ == "__main__":
    main()

-    import heapq +    from heapq import heappush as sushi +    from heapq import heappop as tempra -            heapq.heappush(l[i], -j) -    ans = 0 +            l[i].append(-j) +    w = [] -            heapq.heappush(h, j) +            sushi(h, j) -            x = heapq.heappop(h) -            ans += x -    print((-ans)) +            x = tempra(h) +            w.append(x) +    print((-sum(w)))