#include <bits/stdc++.h>
using namespace std;
int a[100005][11];
int indx[100005][11];
int curIndx[11];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[j][i]);
      indx[a[j][i]][i] = j;
    }
  }
  int pos = 0;
  long long ans = 0;
  while (pos < n) {
    for (int i = 1; i < m; i++) {
      curIndx[i] = indx[a[pos][0]][i];
    }
    int pos2 = pos + 1;
    while (pos2 < n) {
      bool legit = true;
      for (int i = 1; i < m; i++) {
        curIndx[i]++;
        if (curIndx[i] == n || a[curIndx[i]][i] != a[pos2][0]) {
          legit = false;
          break;
        }
      }
      if (!legit) {
        break;
      }
      pos2++;
    }
    long long gap = pos2 - pos;
    ans += (gap * (gap + 1)) >> 1;
    pos = pos2;
  }
  printf("%lld", ans);
  return 0;
}
