#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int A[10][100000];
int Reverse[10][100000 + 1];
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N, M;
  cin >> N >> M;
  for (long long i = 0; i < (long long)M; i++)
    for (long long j = 0; j < (long long)N; j++) {
      cin >> A[i][j];
      Reverse[i][A[i][j]] = j;
    }
  long long ans = 0;
  for (int i = 0; i < N;) {
    long long len = 2;
    for (; i + len - 1 < N; len++) {
      bool flag = true;
      for (int m = 0; m < M; m++) {
        int otheri = Reverse[m][A[0][i]] + len - 1;
        flag &= A[m][otheri] == A[0][i + len - 1];
      }
      if (!flag) break;
    }
    ans += len * (len - 1) / 2;
    i += len - 1;
  }
  cout << ans;
  return 0;
}
