#include <bits/stdc++.h>
using namespace std;
long long pos[11][100001], a[100001], n, m;
long long ans = 0;
bool check(long long x) {
  bool flag = true;
  for (long long i = 2; i <= m; i++)
    if (pos[i][a[x]] != pos[i][a[x - 1]] + 1) return false;
  return true;
}
signed main() {
  scanf("%d %d", &n, &m);
  for (long long i = 1; i <= m; i++)
    for (long long j = 1; j <= n; j++) {
      long long temp;
      scanf("%d", &temp);
      pos[i][temp] = j;
    }
  for (long long i = 1; i <= n; i++) a[pos[1][i]] = i;
  ans += n;
  long long i = 1, j = 2;
  while (j <= n) {
    while (j <= n && check(j)) j++;
    ans += ((j - i) * (j - i - 1)) / 2;
    i = j;
    j = i + 1;
  }
  cout << ans;
}
