#include <bits/stdc++.h>
using namespace std;
const int N = 100050;
int a[N], b[N], na, nb, n, m;
map<int, int> T1, T2;
int Q = 0;
long long ans = 0;
long long C(int x, int y) {
  long long A = 1;
  for (int i = 0; i < y; i++) A = A * (x - i);
  for (int i = 1; i <= y; i++) A = A / i;
  return A;
}
void work1() {
  int get = 0;
  long long num = 0;
  get = 2 * (a[na] - a[1]);
  if (get == 0)
    num = C(na, 3);
  else {
    int x = T1[a[1]], y = T1[a[na]];
    num = C(x, 1) * C(y, 1) * C(na - x - y, 1) + C(x, 2) * C(y, 1) +
          C(x, 1) * C(y, 2);
  }
  if (num) {
    if (get > Q) {
      Q = get;
      ans = num;
    } else if (get == Q)
      ans += num;
  }
}
void work2() {
  int get = 0;
  long long num = 0;
  get = 2 * (b[nb] - b[1]);
  if (get == 0)
    num = C(nb, 3);
  else {
    int x = T2[b[1]], y = T2[b[nb]];
    num = C(x, 1) * C(y, 1) * C(nb - x - y, 1) + C(x, 2) * C(y, 1) +
          C(x, 1) * C(y, 2);
  }
  if (num) {
    if (get > Q) {
      Q = get;
      ans = num;
    } else if (get == Q)
      ans += num;
  }
}
void work3() {
  int get = 2 * n - 2 * (b[1] - a[na]);
  int x = T1[a[na]], y = T2[b[1]];
  long long num = C(x, 1) * C(y, 1) * C(m - x - y, 1) + C(x, 1) * C(y, 2) +
                  C(x, 2) * C(y, 1);
  if (num) {
    if (get > Q) {
      Q = get;
      ans = num;
    } else if (get == Q)
      ans += num;
  }
}
void work4() {
  int get = 2 * n - 2 * (a[1] - b[nb]);
  int x = T2[b[nb]], y = T1[a[1]];
  long long num = C(x, 1) * C(y, 1) * C(m - x - y, 1) + C(x, 1) * C(y, 2) +
                  C(x, 2) * C(y, 1);
  if (num) {
    if (get > Q) {
      Q = get;
      ans = num;
    } else if (get == Q)
      ans += num;
  }
}
void work5() {
  int get = 2 * n;
  long long num = 0;
  for (int i = 1, x = 0, y = 0; i <= na; i++) {
    for (; b[x + 1] < a[i] && x + 1 <= nb; x++)
      ;
    for (; b[y + 1] <= a[i] && y + 1 <= nb; y++)
      ;
    num +=
        C(x, 1) * C(nb - y, 1) + C(y - x, 1) * C(nb - y + x, 1) + C(y - x, 2);
  }
  if (num) {
    if (get > Q) {
      Q = get;
      ans = num;
    } else if (get == Q)
      ans += num;
  }
}
void work6() {
  int get = 2 * n;
  long long num = 0;
  for (int i = 1, x = 0, y = 0; i <= nb; i++) {
    for (; a[x + 1] < b[i] && x + 1 <= na; x++)
      ;
    for (; a[y + 1] <= b[i] && y + 1 <= na; y++)
      ;
    num +=
        C(x, 1) * C(na - y, 1) + C(y - x, 1) * C(na - y + x, 1) + C(y - x, 2);
  }
  if (num) {
    if (get > Q) {
      Q = get;
      ans = num;
    } else if (get == Q)
      ans += num;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int t, x;
    scanf("%d%d", &t, &x);
    if (t == 0) {
      a[++na] = x;
      T1[x]++;
    } else {
      b[++nb] = x;
      T2[x]++;
    }
  }
  sort(a + 1, a + na + 1);
  sort(b + 1, b + nb + 1);
  work1();
  work2();
  if (a[na] < b[1]) work3();
  if (b[nb] < a[1]) work4();
  work5();
  work6();
  printf("%I64d\n", ans);
}
