#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16]={0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline void rd(int &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  char f[10];
  int m=0, s=0;
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class T> struct Heap_max{
  T *val;
  int size;
  void malloc(const int N){
    val = (T*) std::malloc(N*sizeof(T));
    size = 0;
  }
  void walloc(const int N, void **mem = &wmem){
    walloc1d(&val, N, mem);
    size = 0;
  }
  void free(){
    std::free(val);
  }
  void init(){
    size = 0;
  }
  void up(){
    int m, n=size - 1;
    while(n){
      m = (n-1) / 2;
      if(val[m] >= val[n]){
        break;
      }
      swap(val[m], val[n]);
      n = m;
    }
  }
  void down(){
    int m, n=0;
    for(;;){
      m=2*n+1;
      if(m>=size){
        break;
      }
      if(m+1<size && val[m] < val[m+1]){
        m++;
      }
      if(val[m] <= val[n]){
        break;
      }
      swap(val[m], val[n]);
      n=m;
    }
  }
  T top(){
    return val[0];
  }
  T pop(){
    T res=val[0];
    size--;
    if(size > 0){
      val[0] = val[size];
      down();
    }
    return res;
  }
  void push(const T x){
    val[size++] = x;
    up();
  }
}
;
template<class S> void wAdjEdge_L(const int N, const int M, const int *A, const S *B, int **res_sz, S ***res_B, void **mem = &wmem){
  int i, j, k;
  walloc1d(res_sz, N, mem);
  for(i=0;i<(N);i++){
    (*res_sz)[i] = 0;
  }
  for(i=0;i<(M);i++){
    (*res_sz)[A[i]]++;
  }
  walloc1d(res_B, N, mem);
  for(i=0;i<(N);i++){
    walloc1d(&((*res_B)[i]), (*res_sz)[i], mem);
  }
  for(i=0;i<(N);i++){
    (*res_sz)[i] = 0;
  }
  for(i=0;i<(M);i++){
    (*res_B)[A[i]][(*res_sz)[A[i]]++] = B[i];
  }
}
char memarr[96000000];
int N;
int M;
int A[100000];
int B[100000];
int *sz;
int **val;
int main(){
  Heap_max<int> h;
  int i, j, res=0;
  wmem = memarr;
  rd(N);
  rd(M);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=0;Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1);
      rd(B[Lj4PdHRW]);
    }
  }
  wAdjEdge_L(100000, N, A, B, &sz, &val);
  h.walloc(N);
  for(i=0;i<(M);i++){
    for(j=0;j<(sz[i]);j++){
      h.push(val[i][j]);
    }
    if(h.size){
      res += h.pop();
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20190822-2

// --- original code ---
// int N, M, A[1d5], B[1d5];
// int *sz, **val;
// {
//   int i, j, res = 0;
//   Heap_max<int> h;
//   
//   rd(N,M,(A--,B)(N));
//   wAdjEdge(1d5, N, A, B, &sz, &val);
// 
//   h.walloc(N);
// 
//   rep(i,M){
//     rep(j,sz[i]) h.push(val[i][j]);
//     if(h.size) res += h.pop();
//   }
// 
//   wt(res);
// }


Measured memory_bytes: 4352