#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>

#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <random>
#include <complex>
#include <utility>
#include <tuple>
#include <memory>

#include <array>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <csignal>
#include <cstdint>
#include <cstddef>

#ifdef ONLINE_JUDGE
#define NDEBUG
#endif
#include <cassert>
#include <stdexcept>
#include <exception>

// << pair
template <typename T1, typename T2>
std::ostream&operator<<(std::ostream&os,const std::pair<T1,T2>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
// << container
template <typename C>
std::ostream&_debug_go(std::ostream&os,const C&v) {bool first=true;os<<"[";for(auto i:v){if(!first)os<<", ";os<<i;first=false;}return os<<"]";}
// << {vector, set, map} delegating to _debug_go
template<typename T> std::ostream&operator<<(std::ostream&os,const std::vector<T>&v){return _debug_go(os, v);}
template<typename T> std::ostream&operator<<(std::ostream&os,const std::set<T>&v){return _debug_go(os, v);}
template<typename T1, typename T2> std::ostream&operator<<(std::ostream&os,const std::map<T1,T2>&v){return _debug_go(os, v);}
struct Debugger{template<typename T>Debugger&operator,(const T&v){std::cerr<<v<<" "; return *this;}} dbg;
#ifndef NDEBUG
#define debug(args...) do {dbg,args; cerr << "\n";} while(0)
#else
#define debug(args...)
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ITER(i, s, e) for (auto i((s)); i != (e); ++i)
#define IT(i, c) ITER(i, (c).begin(), (c).end())

using namespace std;

using ui = uint32_t;
using ll = int64_t;
using ull = uint64_t;
template <typename T = int> using Pt = complex<T>;

template <typename T>
T cross(const Pt<T>& a, const Pt<T>& b) { return a.real() * b.imag() - a.imag() * b.real(); }

template <typename T>
bool cmpPt(const Pt<T>& a, const Pt<T>& b) {
    return a.real() < b.real() || (a.real() == b.real() && a.imag() < b.imag());
}
// END TEMPLATE

int solve(int N, int M, vector<pair<int, int>> J) {
    sort(J.begin(), J.end());
    int k = 0;
    for (int i = N-1; k < N && i >= 0; ++k, --i) {
        while (i >= 0 && J[i].first + k > M) --i;
        if (i < 0) break;
    }
    int ans = 0;
    set<pair<int, int>> feas;
    for (int i = 0, j = k-1; j >= 0; --j) {
        while (i < N && J[i].first + j <= M) feas.insert({-J[i].second, i}), ++i;
        assert(!feas.empty());
        ans += -feas.begin()->first;
        feas.erase(feas.begin());
    }

    return ans;
}

int solveAltGreedy(int N, int M, vector<pair<int, int>> J) {
    sort(J.begin(), J.end());
    int ans = 0;
    set<pair<int, int>> feas;
    for (int i = 0, j = N-1; j >= 0; --j) {
        while (i < N && J[i].first + j <= M) feas.insert({-J[i].second, i}), ++i;
        if (feas.empty()) continue;
        ans += -feas.begin()->first;
        feas.erase(feas.begin());
    }
    return ans;
}

/*
#include <rapidcheck.h>

int testMain() {
    rc::check("complicated == simple", [] (int M, const vector<pair<int, int>> J) {
        debug(M, J.size(), J);
        if (M <= 0) return;
        int N = J.size();
        RC_ASSERT(solve(N, M, J) == solveAltGreedy(N, M, J));
    });
    return 0;
}
*/

int atCoderMain() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M; cin >> N >> M;
    vector<pair<int, int>> J(N);
    for (auto& j : J) cin >> j.first >> j.second;

    //cout << solve(N, M, J) << endl;
    cout << solveAltGreedy(N, M, J) << endl;

    return 0;
}

int main() {
    return atCoderMain();
}


Measured memory_bytes: 6528