#include <bits/stdc++.h>
using namespace std;
const int le = 1e5;
int a[10][le];
int dp[10][le];
int app[le];
int calc[le];
int main() {
  iostream::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  fill(*dp, *dp + (le * 10), 1);
  fill(calc, calc + le, INT_MAX);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[0][i];
    a[0][i]--;
    app[a[0][i]] = i;
  }
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  for (int k = 0; k < m; k++) {
    for (int i = 0; i < n; i++) {
      if (i - 1 >= 0) dp[k][i] = max(1, dp[k][i - 1] - 1);
      int o = app[a[k][i]];
      while ((o + dp[k][i] < n) && (i + dp[k][i] < n) &&
             a[0][o + dp[k][i]] == a[k][i + dp[k][i]])
        dp[k][i]++;
      calc[o] = min(calc[o], dp[k][i]);
    }
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    res += calc[i];
  }
  cout << res << endl;
  return 0;
}
