#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int N = 1e5 + 7;
int n, m;
int cnt[N];
int ren[N];
int perm[15][N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> perm[i][j];
    }
  }
  for (int i = 1; i <= n; ++i) {
    ren[perm[1][i]] = i;
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      perm[i][j] = ren[perm[i][j]];
    }
  }
  for (int i = 1; i <= n; ++i) {
    cnt[i] = n;
  }
  for (int i = 1; i <= m; ++i) {
    int k = 0;
    for (int j = 1; j <= n; ++j) {
      if (k >= j) {
        cnt[perm[i][j]] = min(cnt[perm[i][j]], k - j + 1);
        continue;
      }
      k = j;
      while (k + 1 <= n && (perm[i][k] + 1 == perm[i][k + 1])) {
        ++k;
      }
      cnt[perm[i][j]] = min(cnt[perm[i][j]], k - j + 1);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += cnt[i];
  }
  cout << ans << "\n";
  return 0;
}
