#include <bits/stdc++.h>
using namespace std;
long long Cnt;
int N, M, HAM, F[2][100005];
void ReNew(int x, long long y) {
  if (y <= 0) return;
  if (x > HAM) HAM = x, Cnt = 0;
  if (x == HAM) Cnt += y;
}
long long C(int a, int b) {
  if (a < b) return 0;
  long long ret = 1;
  for (int i = a - b + 1; i <= a; i++) ret *= i;
  for (int i = 2; i <= b; i++) ret /= i;
  return ret;
}
void Work1(int* A) {
  int l = upper_bound(A + 1, A + A[0] + 1, A[1]) - A - 1,
      r = lower_bound(A + 1, A + A[0] + 1, A[A[0]]) - A - 1;
  if (l == *A)
    ReNew(0, C(A[0], 3));
  else
    ReNew(2 * A[A[0]] - 2 * A[1], 1ll * l * (A[0] - r) * (r - l) +
                                      1ll * C(l, 2) * (A[0] - r) +
                                      1ll * C(A[0] - r, 2) * l);
}
void Work2(int* A, int* B) {
  for (int i = 1; i <= B[0]; i++) {
    int l = lower_bound(A + 1, A + A[0] + 1, B[i]) - A - 1,
        r = lower_bound(A + 1, A + A[0] + 1, B[i]) - A, t;
    t = upper_bound(A + 1, A + A[0] + 1, A[r]) - A - 1;
    ReNew(2 * N - 2 * A[r] + 2 * B[i],
          C(t - r + 1, 2) + 1ll * (A[0] - t) * (t - r + 1));
    ReNew(2 * N, 1ll * l * (A[0] - r + 1));
    t = lower_bound(A + 1, A + A[0] + 1, A[l]) - A;
    ReNew(2 * N - 2 * B[i] + 2 * A[l],
          C(l - t + 1, 2) + 1ll * (t - 1) * (l - t + 1));
  }
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= M; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    F[x][++F[x][0]] = y;
  }
  sort(F[0] + 1, F[0] + F[0][0] + 1);
  sort(F[1] + 1, F[1] + F[1][0] + 1);
  Work1(F[0]);
  Work2(F[0], F[1]);
  Work1(F[1]);
  Work2(F[1], F[0]);
  printf("%I64d\n", Cnt);
}
