#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
pair<int, int> a[MAXN], b[MAXN];
int n, m, Na, Nb;
void Rearrange(pair<int, int> *a, int &n) {
  sort(a + 1, a + n + 1);
  int tot = 0;
  for (int i = 1; i <= n;) {
    int j = i + 1;
    for (; a[j] == a[i] && j <= n; ++j)
      ;
    a[++tot] = make_pair(a[i].first, j - i);
    i = j;
  }
  n = tot;
}
long long calc(int first, int second, int z) {
  return (long long)first * second * z +
         (long long)first * (first - 1) / 2 * second +
         (long long)second * (second - 1) / 2 * first;
}
void Fail(pair<int, int> *a, int N) {
  if (N == 1)
    cout << (long long)a[1].second * (a[1].second - 1) * (a[1].second - 2) / 6
         << endl,
        exit(0);
  int first = a[1].second, second = a[N].second, z = n - first - second;
  cout << calc(first, second, z) << endl;
  exit(0);
}
void Fail(pair<int, int> *a, int Na, pair<int, int> *b, int Nb) {
  cout << calc(a[Na].second, b[1].second, n - a[Na].second - b[1].second)
       << endl;
  exit(0);
}
long long Tohka;
int f[MAXN], g[MAXN], h[MAXN];
void Work(pair<int, int> *a, int Na, pair<int, int> *b, int Nb) {
  memset(f, 0, sizeof(f));
  memset(g, 0, sizeof(g));
  memset(h, 0, sizeof(h));
  for (int i = Na, j = Nb, s = 0; i >= 1; f[i] = s, --i)
    for (; b[j].first >= a[i].first && j >= 1; s += b[j].second, --j)
      h[i] += (b[j].first == a[i].first) * b[j].second;
  for (int i = 1, j = 1, s = 0; i <= Na; g[i] = s, ++i)
    for (; b[j].first <= a[i].first && j <= Nb; s += b[j].second, ++j)
      ;
  for (int i = 1; i <= Na; ++i) {
    int first = f[i] - h[i], second = g[i] - h[i], z = a[i].second;
    Tohka += (long long)z * first * second +
             (long long)z * h[i] * (first + second) +
             (long long)z * h[i] * (h[i] - 1) / 2;
  }
}
int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= n; ++i) {
    int ty, first;
    scanf("%d%d", &ty, &first);
    if (first == m) first = 0, ty ^= 1;
    (ty ? (b[++Nb]) : (a[++Na])) = make_pair(first, 0);
  }
  Rearrange(a, Na);
  Rearrange(b, Nb);
  if (!Nb) Fail(a, Na);
  if (!Na) Fail(b, Nb);
  if (a[Na].first < b[1].first) Fail(a, Na, b, Nb);
  if (a[1].first > b[Nb].first) Fail(b, Nb, a, Na);
  Work(a, Na, b, Nb);
  Work(b, Nb, a, Na);
  cout << Tohka << endl;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
