#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(int i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 1e18
#define five 100005;
#define six 1000005;
#define prllclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
//___________________________________________________________________________________________________________________________________________________________
const int N = five;
map<ll,vct> mp;
struct tree{
       int maxi;
       vct s;
       int curr;
};
tree* seg[4*N];
void build(int ind,int rl,int rr){
       seg[ind] = new tree();
       if(rl==rr){
              for(int i : mp[rl]){
                            seg[ind]->s.pb(i);
              }
              sort(all(seg[ind]->s));
              if(seg[ind]->s.size()){
                     seg[ind]->maxi = seg[ind]->s[seg[ind]->s.size()-1];
              }
              else{
                     seg[ind]->maxi = 0;
              }
              seg[ind]->curr = rl;
              return;
       }
       int mid = (rl+rr)/2;
       build(2*ind,rl,mid);
       build(2*ind+1,mid+1,rr);
       seg[ind]->maxi = max(seg[2*ind+1]->maxi,seg[2*ind]->maxi);
       seg[ind]->curr = seg[2*ind+1]->curr;
       if(seg[ind]->maxi != seg[2*ind+1]->maxi){seg[ind]->curr = seg[2*ind]->curr;}
}
void update(int ind,int rl,int rr,int ix)
{
       if(rl == rr)
       {
              if(seg[ind]->s.size()){
                     seg[ind]->s.pop_back();
                     if(seg[ind]->s.size()){
                     seg[ind]->maxi = seg[ind]->s[seg[ind]->s.size()-1];}
                     else{
                            seg[ind]->maxi = 0;
                     }
              }
              return;
       }
       int mid = (rl+rr)/2;
       if(ix>=rl&&mid>=ix){
              update(2*ind,rl,mid,ix);
       }
       else{
              update(2*ind+1,mid+1,rr,ix);
       }
       seg[ind]->maxi = max(seg[2*ind+1]->maxi,seg[2*ind]->maxi);
       seg[ind]->curr = seg[2*ind+1]->curr;
       if(seg[ind]->maxi != seg[2*ind+1]->maxi){seg[ind]->curr = seg[2*ind]->curr;}
}
pll q(int ind,int rl,int rr,int ql,int qr)
{
       if(rl>qr || ql > rr){
              return {0,0};
       }
       if(rl>=ql && rr<=qr){
              return{seg[ind]->maxi,seg[ind]->curr};
       }
       int mid = (rl+rr)/2;
       pll x1 = q(2*ind,rl,mid,ql,qr);
       pll x2 = q(2*ind+1,mid+1,rr,ql,qr);
       if(x1.fr>x2.fr){
              return x1;
       }
       return x2;
}
int main(){
       int n,m;
       cin>>n>>m;
       pll a[n];
       int b[n];
       fri(i,0,n){
              int x,y;
              cin>>x>>y;
              mp[x].pb(y);
       }
       build(1,0,m+2);
       ll sum = 0;
       for(int i = 1 ; i <= m ; i++){
              pll xx = q(1,0,m+2,1,i);
              sum += xx.fr;
              update(1,0,m+2,xx.sc);
       }
       cout<<sum<<endl;
}


Measured memory_bytes: 26496