#include <bits/stdc++.h>
using namespace std;
int perm[10][1000000];
int pos[10][1000000];
bool good(int n, int m, int base, int d) {
  if (pos[0][base] + d >= n) return false;
  int num = perm[0][pos[0][base] + d];
  for (int i = 1; i < m; i++) {
    int p = pos[i][base] + d;
    if (p >= n) return false;
    if (perm[i][p] != num) return false;
  }
  return true;
}
int main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      a--;
      perm[i][j] = a;
      pos[i][a] = j;
    }
  }
  long long ans = 0;
  int len = 1;
  for (int i = 0; i < n; i++) {
    while (good(n, m, perm[0][i], len)) len++;
    ans += len;
    len = max(1, len - 1);
  }
  cout << ans << "\n";
  return 0;
}
