#include <bits/stdc++.h>
using namespace std;
int64_t n, m;
vector<int64_t> d;
vector<vector<int64_t> > nxt;
int64_t rec(int64_t ind) {
  if (ind == n) return 0;
  if (d[ind]) return d[ind];
  int64_t u = nxt[0][ind];
  if (u == -1) return 1;
  for (int64_t i = 0; i < m - 1; ++i)
    if (nxt[i + 1][ind] != u) return 1;
  d[ind] = 1 + rec(u);
  return d[ind];
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  nxt.assign(m, vector<int64_t>(n, -1));
  d.resize(n);
  int64_t lst = 0;
  for (int64_t i = 0; i < m; ++i)
    for (int64_t j = 0; j < n; ++j) {
      int64_t a;
      cin >> a;
      --a;
      if (j) nxt[i][lst] = a;
      lst = a;
    }
  int64_t ans = 0;
  for (int64_t i = 0; i < n; ++i) ans += rec(i);
  cout << ans;
}
