#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int dia, n;
long long a[N], ans;
map<long long, int> mp;
int main() {
  cin >> dia >> n;
  ans = (long long)n * (n - 1LL) * (n - 2LL) / 6LL;
  for (int i = 1, s, f; i <= n; ++i) {
    cin >> s >> f;
    if (!s) f += dia;
    f %= 2 * dia;
    a[i] = f;
  }
  sort(a + 1, a + n + 1);
  for (int i = n + 1; i <= 2 * n; ++i) {
    a[i] = a[i - n] + 2 * dia;
    mp[a[i]]++;
    mp[a[i - n]]++;
  }
  for (int i = 1, ni = 1; i <= n; ++i) {
    for (; a[ni + 1] - a[i] < dia; ++ni)
      ;
    ans -= (long long)(ni - i) * (ni - i - 1LL) / 2LL;
  }
  if (ans) {
    cout << ans << '\n';
    return 0;
  }
  long long l = 0;
  for (int i = 1, ni; i <= n; ++i) {
    for (; a[ni + 1] - a[i] < dia; ++ni)
      ;
    if (ni - i > 1) l = max(l, a[ni] - a[i]);
  }
  for (int i = 1, ni = 1; i <= n; ++i) {
    ni = max(ni, i);
    mp[a[i]]--;
    for (; a[ni + 1] - a[i] < l; ++ni)
      ;
    if (a[ni + 1] - a[i] == l) {
      if (ni + 1 - i > 1) ans += (long long)(ni - i) * mp[a[ni + 1]];
      ans += (long long)mp[a[ni + 1]] * (mp[a[ni + 1]] - 1) / 2LL;
    }
  }
  cout << ans << '\n';
}
