#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 5;
void solve() {
  long long int n, m, i, j;
  cin >> n >> m;
  long long int a[m][n];
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  long long int rename[n + 1];
  for (i = 0; i < n; i++) {
    rename[a[0][i]] = i + 1;
  }
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      a[i][j] = rename[a[i][j]];
    }
  }
  vector<long long int> reach(n + 1, LLONG_MAX);
  for (i = 0; i < m; i++) {
    long long int temp;
    j = 0;
    while (j < n) {
      temp = a[i][j];
      long long int k = 0;
      while (j < n && temp + k == a[i][j]) {
        j++;
        k++;
      }
      long long int l = 0;
      while (l < k) {
        reach[temp + l] = min(reach[temp + l], k - l);
        l++;
      }
    }
  }
  long long int ans = 0;
  for (i = 1; i <= n; i++) {
    if (reach[i] != LLONG_MAX)
      ans += reach[i];
    else
      ans++;
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  long long int t = 1;
  while (t--) {
    solve();
  }
}
