#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:255000000")
bool firstout = 1;
template <class T>
T &minn(T &a, T b) {
  if (b < a) a = b;
  return a;
}
template <class T>
T &maxx(T &a, T b) {
  if (a < b) a = b;
  return a;
}
int &madd(int &a, int b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
  return a;
}
int &msub(int &a, int b) {
  a -= b;
  if (a < 0) a += 1000000007;
  return a;
}
int &mmult(int &a, int b) { return a = (long long)a * b % 1000000007; }
int mdiv(long long a, long long b, long long m) {
  a = (a % m + m) % m;
  b = (b % m + m) % m;
  if (a % b == 0) return a / b;
  return (a + m * mdiv(-a, m, b)) / b;
}
int n, m, q;
int A[101234];
long long calc(long long n) {
  long long res = n * (n - 1) / 2;
  if (n & 1) return res + 1;
  return res + n / 2;
}
int main() {
  int i, j, k;
  char c;
  int a, d;
  int ts;
  for (ts = 1; scanf("%"
                     "d",
                     &(q)) > 0;
       ++ts) {
    scanf(
        "%"
        "d",
        &(n));
    for (i = (0); i < (n); ++i)
      scanf(
          "%"
          "d",
          &(a)),
          scanf(
              "%"
              "d",
              &(A[i]));
    sort(A, A + n);
    reverse(A, A + n);
    for (i = (n + 1) - 1; i >= (1); --i)
      if (calc(i) <= q) break;
    long long res = 0;
    for (j = (0); j < (i); ++j) res += A[j];
    printf(
        "%"
        "I64d",
        (res));
    printf("\n"), firstout = 1;
  }
  return 0;
}
