// inputに入力データ全体が入る
function Main(input) {
	// 1行目がinput[0], 2行目がinput[1], …に入る
	input = input.split("\n");
  var tmp = input[0].trim().split(' ');
  var n = parseInt(tmp[0], 10);
  var m = parseInt(tmp[1], 10);


  var table = [];
  for (var i = 1; i < input.length; i++) {
    var tmp = separateSpace(input[i]);
    table[i-1] = toIntAll(tmp);
  }

  table = sortByDay(table);

  // console.log(table);
  var info = {};
  info.sum =0;
  info.sliceStartIndex = 0;
  info.cursorDay = 1;
  info.day = 1;

  var checkTable = []
  for (info.day = 1; info.day <=n ; info.day++) {
    info.cursorStartDay = table[info.sliceStartIndex][0];

    if(info.cursorStartDay == info.day){
      for (var i = info.sliceStartIndex; i < table.length; i++) {
        info.cursorDay = table[i][0];

        if(  (info.cursorDay != info.day) || ( i == (table.length-1) )  ){
          var sliceLast = ( info.sliceStartIndex == (table.length-1) ) ? i+1 : i;

          var tmpTable = table.slice(info.sliceStartIndex, sliceLast);
          checkTable = checkTable.concat(tmpTable);

          checkTable = sortByReward(checkTable);

          info.sliceStartIndex = i;
          // console.log(info);
          break;
        }
      }
    }


    // console.log("day:"+info.day);
    if(checkTable.length > 0){
      // console.log(checkTable);
      var choice = checkTable.pop();
      // console.log(choice);
      info.sum += choice[1];
    }
  }

	//文字列から10進数に変換するときはparseIntを使います

	//出力
	console.log('%d',info.sum);
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

function sortByDay(table){
  return table.sort(function(a,b){
    if(a[0] - b[0] > 0) return 1;
    if(a[0] - b[0] < 0) return -1;
    return b[1] - a[1];
  });
}
function sortByReward(table){
  return table.sort(function(a,b){
    if(a[1] - b[1] > 0) return 1;
    if(a[1] - b[1] < 0) return -1;
    return b[0] - a[0];
  });
}

function toIntAll(input){
  var out = []
  for(var i=0; i<input.length; i++){
    out[i] = parseInt(input[i]);
  }
  return out;
}

//文字列をスペースで分割 -> array
function separateSpace(input){
  return input.trim().split(' ');
}
