#include <bits/stdc++.h>
using namespace std;
const int maxm = 5e5 + 5;
int N, M;
pair<int, int> A[maxm];
inline int operator-(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first != b.first ? a.second + N - b.second
         : a >= b           ? a.second - b.second
                            : N - b.second + N + a.second;
}
inline long long count(int x, int y) {
  long long len = (y - x + M) % M + 1;
  if (len < 3) return 0;
  return len * (len - 1) * (len - 2) / 6;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= M; i++) scanf("%d%d", &A[i].first, &A[i].second);
  sort(A + 1, A + M + 1);
  int st(1), en(1);
  long long sum = (long long)M * (M - 1) * (M - 2) / 6;
  if (A[1] < A[M])
    for (int i = 1, j = 1; i <= M; i++) {
      while ((A[j] == A[i] || A[j % M + 1] != A[i]) && A[j % M + 1] - A[i] < N)
        j = j % M + 1;
      if ((j - i + M) % M > 1)
        sum -= (long long)((j - i + M) % M) * ((j - i - 1 + M) % M) / 2;
      if ((j - i + M) % M > (en - st + M) % M) st = i, en = j;
      if (j == i) j++;
    }
  if (!sum) {
    int st2(st), en2(en);
    while (A[st2 % M + 1] == A[st2]) st2 = st2 % M + 1;
    while (A[(en2 - 2 + M) % M + 1] == A[en2]) en2 = (en2 - 2 + M) % M + 1;
    sum = count(st, en) - count(st, en2 - 1) - count(st2 + 1, en) +
          (st2 % M + 1 != en2 ? count(st2 + 1, en2 - 1) : 0);
  }
  printf("%I64d\n", sum);
  return 0;
}
