#include <bits/stdc++.h>
using namespace std;
int n, m;
long long num[2];
map<int, int> cnt[2];
void check_val(int &best_dist, long long &best_total, int dist,
               long long total) {
  if (total == 0) return;
  if (dist > best_dist) best_dist = dist, best_total = 0;
  if (dist == best_dist) best_total += total;
}
pair<int, long long> calc_equal(int i) {
  long long k = num[i];
  auto &a = cnt[i];
  if (a.size() == 1) return make_pair(0, k * (k - 1) * (k - 2) / 6);
  auto it0 = a.begin();
  auto it1 = --a.end();
  int dist = 2 * (it1->first - it0->first);
  long long cnt0 = it0->second, cnt1 = it1->second;
  long long total = cnt0 * cnt1 * (k - cnt0 - cnt1);
  total += cnt0 * (cnt0 - 1) / 2 * cnt1;
  total += cnt1 * (cnt1 - 1) / 2 * cnt0;
  return make_pair(dist, total);
}
pair<int, long long> calc_unequal(int i, int j) {
  long long ki = num[i], kj = num[j];
  auto &ai = cnt[i], &aj = cnt[j];
  int dist = -1;
  long long total = 0;
  long long prev = 0;
  auto it = ai.begin();
  for (auto &val : aj) {
    while (it != ai.end() && it->first <= val.first) prev += it->second, it++;
    if (it != ai.end()) {
      int cur_dist = 2 * (n - it->first + val.first);
      long long k = it->second;
      long long cur_total =
          val.second * (k * (k - 1) / 2 + k * (ki - prev - k));
      check_val(dist, total, cur_dist, cur_total);
    }
  }
  prev = 0;
  it = ai.begin();
  for (auto &val : aj) {
    while (it != ai.end() && it->first < val.first) prev += it->second, it++;
    if (it != ai.begin()) it--, prev -= it->second;
    if (it->first < val.first) {
      int cur_dist = 2 * (n - val.first + it->first);
      long long k = it->second;
      long long cur_total = val.second * (k * (k - 1) / 2 + k * prev);
      check_val(dist, total, cur_dist, cur_total);
    }
  }
  prev = 0;
  it = ai.begin();
  for (auto &val : aj) {
    while (it != ai.end() && it->first < val.first) prev += it->second, it++;
    if (it != ai.end()) {
      int cur_dist = 2 * n;
      long long k = it->second;
      long long cur_total = prev * (ki - prev);
      if (it->first == val.first)
        cur_total += k * (k - 1) / 2 + k * (ki - prev - k);
      cur_total *= val.second;
      check_val(dist, total, cur_dist, cur_total);
    }
  }
  return make_pair(dist, total);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int s, f;
    scanf("%d%d", &s, &f);
    num[s]++;
    cnt[s][f]++;
  }
  vector<pair<int, long long>> all;
  for (int i = 0; i < 2; i++)
    if (num[i] > 2) all.push_back(calc_equal(i));
  for (int i = 0; i < 2; i++)
    if (num[i] > 1 && num[1 - i] > 0) all.push_back(calc_unequal(i, 1 - i));
  int dist = -1;
  long long total = 0;
  for (auto val : all) check_val(dist, total, val.first, val.second);
  printf("%lld\n", total);
}
