#include <bits/stdc++.h>
using namespace std;
int len, ntot;
int n[2], pref[2][100000];
int maxval;
long long cntmaxval;
void found(int val, long long cnt) {
  if (cnt <= 0) return;
  if (val > maxval) {
    maxval = val;
    cntmaxval = 0;
  }
  if (val == maxval) cntmaxval += cnt;
}
void run() {
  scanf("%d%d", &len, &ntot);
  n[0] = n[1] = 0;
  for (int i = (0); i < (ntot); ++i) {
    int s, t;
    scanf("%d%d", &s, &t);
    pref[s][n[s]++] = t;
  }
  for (int i = (0); i < (2); ++i) sort(pref[i], pref[i] + n[i]);
  maxval = -1;
  cntmaxval = 0;
  for (int t = (0); t < (2); ++t)
    if (n[t] >= 3) {
      int a = pref[t][0], c = pref[t][n[t] - 1];
      if (pref[t][0] == pref[t][n[t] - 1]) {
        found(2 * (c - a), (long long)n[t] * (n[t] - 1) * (n[t] - 2) / 6);
        continue;
      }
      int u = 0;
      int v = 0;
      while (u < n[t] && pref[t][u] <= pref[t][0]) ++u;
      while (v < n[t] && pref[t][v] < pref[t][n[t] - 1]) ++v;
      int na = u, nc = n[t] - v, nb = n[t] - na - nc;
      found(2 * (c - a), (long long)na * nb * nc +
                             (long long)na * (na - 1) / 2 * nc +
                             (long long)na * nc * (nc - 1) / 2);
    }
  for (int t = (0); t < (2); ++t)
    if (n[1 - t] >= 2) {
      int v = 0;
      int w = 0;
      int u = 0;
      for (int i = (0); i < (n[t]); ++i) {
        int a = pref[t][i];
        while (v < n[1 - t] && pref[1 - t][v] < a) ++v;
        while (w < n[1 - t] && (w < v || pref[1 - t][w] <= pref[1 - t][v])) ++w;
        while (u < n[1 - t] && v - 1 >= 0 &&
               pref[1 - t][u] < pref[1 - t][v - 1])
          ++u;
        if (v < n[1 - t]) {
          int b = pref[1 - t][v], nequal = w - v, ngreater = n[1 - t] - w;
          found(2 * (a + len - b), (long long)nequal * ngreater +
                                       (long long)nequal * (nequal - 1) / 2);
        }
        {
          int nb = v, nc = n[1 - t] - v;
          found(2 * len, (long long)nb * nc);
        }
        if (v - 1 >= 0) {
          int c = pref[1 - t][v - 1], nequal = v - u, nsmaller = u;
          found(2 * (c + len - a), (long long)nequal * nsmaller +
                                       (long long)nequal * (nequal - 1) / 2);
        }
      }
    }
  cout << cntmaxval << endl;
}
int main() {
  run();
  return 0;
}
