#include <bits/stdc++.h>
using namespace std;
int n, m;
long long q[3000000], w[3000000], dp[3000000];
pair<long long, long long> q1[3000000];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%I64d%I64d", &q[i], &w[i]);
  for (int i = 1; i <= m; i++) q1[i] = make_pair(w[i], q[i]);
  sort(q1 + 1, q1 + m + 1);
  reverse(q1 + 1, q1 + m + 1);
  dp[1] = 1ll;
  long long k = 1ll;
  for (int i = 2; i <= 2000000; i++) {
    dp[i] = dp[i - 1] + k;
    k += 1ll;
  }
  k = 0;
  for (int i = 1; i <= 2000000; i++)
    if (i % 2 == 0) {
      dp[i] += k;
      k += 1ll;
    }
  long long ans = 0, sum = 0;
  for (int i = 1; i <= m; i++) {
    if (dp[i] > n) break;
    sum += q1[i].first;
    ans = sum;
  }
  cout << ans;
  return 0;
}
