#include <bits/stdc++.h>
using namespace std;
int a[15][100005];
int pos[15][100005];
int n, m;
bool visited[100005];
long long ans[100005];
int x[15];
void AAA(int i) {
  ans[i] = 1ll;
  bool dali = 1;
  for (int j = 0; j < m; j++) {
    x[j] = pos[j][i];
    if (x[j] == n) dali = 0;
  }
  if (dali) {
    int nxt0 = a[0][x[0] + 1];
    for (int j = 1; j < m; j++) {
      if (a[j][x[j] + 1] != nxt0) {
        dali = 0;
        break;
      }
    }
    if (dali) {
      if (visited[nxt0])
        ans[i] = ans[nxt0] + 1ll;
      else {
        visited[nxt0] = 1;
        AAA(nxt0);
        ans[i] = ans[nxt0] + 1ll;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      pos[i][a[i][j]] = j;
    }
  }
  long long sum = 0ll;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      visited[i] = 1;
      AAA(i);
    }
    sum += ans[i];
  }
  cout << sum << '\n';
  return 0;
}
