#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, z, i, j, lt, x, t, y, d, r, q, m, k;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s;
  cin >> n >> m;
  long long ar[m][n];
  long long nxt[m][n + 1];
  for (i = 0; i < m; i++) {
    cin >> x;
    ar[i][0] = x;
    nxt[i][0] = x;
    for (j = 1; j < n; j++) {
      cin >> x;
      ar[i][j] = x;
      nxt[i][ar[i][j - 1]] = x;
    }
    nxt[i][ar[i][j - 1]] = 0;
  }
  long long ans = n;
  for (i = 0; i < n;) {
    x = ar[0][i];
    z = i;
    i++;
    while (i < n) {
      y = nxt[0][x];
      for (k = 1; k < m; k++) {
        if (nxt[k][x] != y) break;
      }
      if (k != m) break;
      i++;
      x = y;
    }
    k = z - i;
    ans += (k * (k + 1ll)) / 2ll;
  }
  cout << ans << endl;
}
