#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(m, vector<int>(n + 1));
  for (int i = 0; i < m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  vector<int> ren(n + 1);
  for (int i = 1; i <= n; i++) {
    ren[a[0][i]] = i;
  }
  for (int i = 0; i < m; i++) {
    for (int j = 1; j <= n; j++) {
      a[i][j] = ren[a[i][j]];
    }
  }
  vector<int> len(n + 1, n);
  for (int i = 0; i < m; i++) {
    int cur = 1;
    for (int j = 1; j <= n; j++) {
      if (cur < j) cur++;
      while (cur < n && a[i][cur + 1] == a[i][cur] + 1) cur++;
      len[a[i][j]] = min(len[a[i][j]], a[i][cur]);
    }
  }
  long long ans = 0;
  int now = 1;
  while (now <= n) {
    int cur = len[now] - now + 1;
    ans += (cur + 1LL) * cur / 2LL;
    now = len[now] + 1;
  }
  cout << ans << '\n';
  return 0;
}
