#include <bits/stdc++.h>
using namespace std;
int n, m, a[110000], b[110000], ans = 0, l[110000], r[110000];
long long sum = 0;
void change(int t1, long long t2) {
  if (t1 > ans)
    ans = t1, sum = t2;
  else if (t1 == ans)
    sum += t2;
}
void solved() {
  l[1] = 1;
  for (int i = 2; i <= a[0]; i++)
    if (a[i] == a[i - 1])
      l[i] = l[i - 1] + 1;
    else
      l[i] = 1;
  r[a[0]] = 1;
  for (int i = a[0] - 1; i; i--)
    if (a[i] == a[i + 1])
      r[i] = r[i + 1] + 1;
    else
      r[i] = 1;
  long long tt;
  if (a[0] > 2) {
    if (r[1] == a[0])
      tt = (long long)a[0] * (a[0] - 1) * (a[0] - 2) / 6;
    else
      tt = (long long)l[a[0]] * (l[a[0]] - 1) * r[1] / 2 +
           (long long)r[1] * (r[1] - 1) * l[a[0]] / 2 +
           (long long)l[a[0]] * r[1] * (a[0] - l[a[0]] - r[1]);
    change((a[a[0]] - a[1]) * 2, tt);
  }
  l[1] = 1;
  for (int i = 2; i <= b[0]; i++)
    if (b[i] == b[i - 1])
      l[i] = l[i - 1] + 1;
    else
      l[i] = 1;
  r[b[0]] = 1;
  for (int i = b[0] - 1; i > 0; i--)
    if (b[i] == b[i + 1])
      r[i] = r[i + 1] + 1;
    else
      r[i] = 1;
  int w = b[0];
  for (int i = 1; i <= a[0]; i++) {
    while (w && a[i] + b[w] > n) w--;
    if (w >= 2) {
      if (l[w] == w)
        tt = (long long)w * (w - 1) / 2;
      else
        tt = (long long)l[w] * (w - l[w]) + (long long)l[w] * (l[w] - 1) / 2;
      change((a[i] + b[w]) * 2, tt);
    }
    if (w >= 1 && w < b[0]) change(n * 2, (long long)w * (b[0] - w));
    if (w < b[0] - 1) {
      if (r[w + 1] == b[0] - w)
        tt = (long long)r[w + 1] * (r[w + 1] - 1) / 2;
      else
        tt = (long long)r[w + 1] * (b[0] - w - r[w + 1]) +
             (long long)r[w + 1] * (r[w + 1] - 1) / 2;
      change(n * 2 - (a[i] + b[w + 1] - n), tt);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (x)
      a[++a[0]] = y;
    else
      b[++b[0]] = n - y;
  }
  if (a[0]) sort(a + 1, a + a[0] + 1);
  if (b[0]) sort(b + 1, b + b[0] + 1);
  if (a[0]) solved();
  for (int i = max(a[0], b[0]); i >= 0; i--) swap(a[i], b[i]);
  if (a[0]) solved();
  cout << sum << endl;
  return 0;
}
