#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, i, j, x, y, childcnt[100005], child[100005], parent[100005];
  long long res = 0, cnt;
  vector<int> v;
  bool vis[100005];
  for (i = 0; i < 100005; i++) childcnt[i] = 0;
  for (i = 0; i < 100005; i++) child[i] = -1;
  cin >> n >> m;
  for (j = 0; j < m; j++) {
    for (i = 0; i < n; i++) {
      cin >> x;
      if (i != 0) {
        if (child[y] != x) childcnt[y]++;
        child[y] = x;
        parent[x] = y;
      } else
        parent[x] = -1;
      y = x;
    }
    childcnt[x]++;
  }
  if (m == 1) {
    cnt = n;
    cout << (cnt * (cnt + 1)) / 2 << endl;
    return 0;
  }
  for (i = 1; i <= n; i++) {
    if (childcnt[i] == 1 && (parent[i] == -1 || childcnt[parent[i]] != 1))
      v.push_back(i);
  }
  memset(vis, false, sizeof(vis));
  for (i = 0; i < v.size(); i++) {
    x = v[i];
    cnt = 1;
    while (!vis[x] && childcnt[x] == 1) {
      vis[x] = true;
      x = child[x];
      cnt++;
    }
    res += ((cnt * (cnt + 1)) / 2 - cnt);
  }
  cout << res + n << endl;
  return 0;
}
