#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, n, m, a;
  long long total;
  scanf("%d %d", &n, &m);
  int p[m][n], inv[m][n], l[m], r[m];
  for (j = 0; j < m; j++) {
    for (i = 0; i < n; i++) {
      scanf("%d", &p[j][i]);
      p[j][i]--;
      inv[j][p[j][i]] = i;
    }
  }
  total = 0;
  l[0] = r[0] = 0;
  while (r[0] < n) {
    l[0] = r[0];
    for (j = 1; j < m; j++) {
      l[j] = inv[j][p[0][l[0]]];
      r[j] = l[j];
    }
    bool perm = true;
    while (perm) {
      r[0]++;
      if (r[0] >= n) break;
      for (j = 1; j < m; j++) {
        r[j]++;
        if (r[j] >= n || p[j][r[j]] != p[0][r[0]]) {
          perm = false;
          break;
        }
      }
    }
    long long aux = r[0] - l[0];
    total = total + aux * (aux + 1ll) / 2ll;
  }
  cout << total << endl;
}
