#include <bits/stdc++.h>
using namespace std;
long long CON = 111111;
long long n, m, kol = 0;
vector<long long> pr(100100);
vector<long long> used(100100, 0);
vector<vector<long long> > sm(100100);
map<long long, long long> maps;
void dfs(long long v) {
  used[v] = 1;
  ++kol;
  for (long long i = 0; i < (long long)sm[v].size(); i++) {
    if (used[sm[v][i]] == 0) dfs(sm[v][i]);
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  if (n == 1) {
    cout << 1;
    return 0;
  }
  for (long long k = 1; k <= m; k++) {
    for (long long i = 1; i <= n; i++) {
      cin >> pr[i];
    }
    for (long long i = 1; i <= n - 1; i++) {
      maps[pr[i] * CON + pr[i + 1]]++;
    }
  }
  long long a1, a2;
  for (auto now : maps) {
    if (now.second == m) {
      a1 = now.first / CON;
      a2 = now.first % CON;
      sm[a1].push_back(a2);
      sm[a2].push_back(a1);
    }
  }
  long long ans = 0, del = 0;
  for (long long i = 1; i <= n; i++) {
    if (used[i] == 0) {
      del = kol;
      dfs(i);
      del = kol - del;
      ans += (del * del + del) / 2;
    }
  }
  cout << ans;
  return 0;
}
