#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > tab;
vector<int> nast, pop;
int n, m;
int dfs(int a) {
  if (nast[a] == -1)
    return 1;
  else
    return 1 + dfs(nast[a]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  tab.resize(m);
  nast.resize(n + 1, -1);
  pop.resize(n + 1, -1);
  int a, b;
  cin >> a;
  for (int i = 1; i < n; i++) {
    cin >> b;
    nast[a] = b;
    a = b;
  }
  for (int i = 1; i < m; i++) {
    ("check\n");
    cin >> a;
    for (int j = 1; j < n; j++) {
      cin >> b;
      if (nast[a] != b) nast[a] = -1;
      a = b;
    }
    nast[a] = -1;
  }
  for (int i = 1; i < n + 1; i++)
    if (nast[i] > 0) pop[nast[i]] = i;
  long long wyn = n, x;
  for (int i = 1; i < n + 1; i++)
    if (pop[i] == -1) {
      x = dfs(i);
      wyn += ((x - 1) * x) / 2;
    }
  cout << wyn << "\n";
}
