#include <bits/stdc++.h>
using namespace std;
int numpos[11][100001];
int a[11][100001];
int ans[100001];
int n, m;
void solve(int num) {
  if (!num) return;
  ans[num] = 1;
  int pos = numpos[1][num];
  int q = a[1][pos + 1];
  for (int i = 2; i <= m; i++) {
    pos = numpos[i][num];
    if (q != a[i][pos + 1]) {
      return;
    }
  }
  if (!ans[q]) solve(q);
  ans[num] += ans[q];
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);
      numpos[i][a[i][j]] = j;
    }
  }
  for (int i = 1; i <= n; i++) ans[i] = 0;
  solve(2);
  long long answer = 0;
  for (int i = 1; i <= n; i++) {
    if (!ans[i]) {
      solve(i);
    }
    answer += ans[i];
  }
  cout << answer;
  return 0;
}
