#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int **A = new int *[m];
  int *B = new int[n];
  int *reach = new int[n];
  for (int i = 0; i < m; i++) {
    A[i] = new int[n]();
    for (int j = 0; j < n; j++) {
      reach[j] = n - 1;
      scanf("%d", &A[i][j]);
      A[i][j]--;
      if (i == 0) B[A[0][j]] = j;
    }
  }
  for (int i = 0; i < m; i++) {
    int right = 0;
    for (int left = 0; left < n; left++) {
      if (right < left) right++;
      while ((right < n - 1) && (B[A[i][right + 1]] == B[A[i][right]] + 1))
        right++;
      if (B[A[i][right]] < reach[A[i][left]])
        reach[A[i][left]] = B[A[i][right]];
    }
  }
  long long S = 0;
  for (int i = 0; i < n; i++) {
    reach[i] -= i - 1;
    S += reach[i];
  }
  cout << S << endl;
  for (int i = 0; i < m; i++) {
    delete[] A[i];
  }
  delete[] A;
  delete[] B;
  delete[] reach;
}
