#include <bits/stdc++.h>
using namespace std;
int n, m, a[15][100005], posa[15][100005];
set<int> vis;
int countder(int x) {
  if (posa[0][x] == n - 1)
    return 0;
  else {
    int der = a[0][posa[0][x] + 1];
    bool can = 1;
    for (int i = 1; i < m; i++) {
      if (posa[i][x] == n - 1)
        can = 0;
      else if (a[i][posa[i][x] + 1] != der)
        can = 0;
    }
    if (can) {
      vis.insert(der);
      return countder(der) + 1;
    }
    return 0;
  }
}
int countizq(int x) {
  if (posa[0][x] == 0)
    return 0;
  else {
    int izq = a[0][posa[0][x] - 1];
    bool can = 1;
    for (int i = 1; i < m; i++) {
      if (posa[i][x] == 0)
        can = 0;
      else if (a[i][posa[i][x] - 1] != izq)
        can = 0;
    }
    if (can) {
      vis.insert(izq);
      return countizq(izq) + 1;
    }
    return 0;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
      posa[i][a[i][j]] = j;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis.find(i) != vis.end()) continue;
    int act = i;
    int d = countizq(act) + countder(act) + 1;
    ans += ((d + 1) * 1LL * (d)) / 2;
  }
  printf("%lld\n", ans);
  return 0;
}
