#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  long long n, m, i, j;
  cin >> n >> m;
  long long a[n], pos[n + 1][m], x;
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      cin >> x;
      pos[x][i] = j;
      if (i == 0) a[j] = x;
    }
  }
  long long ans = 0, k;
  i = 0;
  while (i < n) {
    j = i + 1;
    while (j < n) {
      bool poss = true;
      for (k = 1; k < m && poss; k++) {
        if (pos[a[j]][k] - pos[a[j - 1]][k] != 1) poss = false;
      }
      if (poss)
        j++;
      else
        break;
    }
    j--;
    ans += ((j - i + 2) * (j - i + 1)) / 2;
    i = ++j;
  }
  cout << ans << endl;
  return 0;
}
