#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
const ull seed = 1333331;
int n, m, a[11][100010];
int pos[11][100010], r[100010];
ull P[100010], ha[11][100010];
ull get(int x, int l, int r) { return ha[x][r] - ha[x][l - 1] * P[r - l + 1]; }
bool check(int c, int len) {
  int i;
  ull tmp = get(1, pos[1][c], pos[1][c] + len - 1);
  for (i = 2; i <= m; i++) {
    if (tmp != get(i, pos[i][c], pos[i][c] + len - 1)) return false;
  }
  return true;
}
int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) r[i] = n;
  P[0] = 1;
  for (i = 1; i <= n; i++) P[i] = P[i - 1] * seed;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);
      ha[i][j] = ha[i][j - 1] * seed + a[i][j];
      pos[i][a[i][j]] = j;
      r[a[i][j]] = min(r[a[i][j]], n - j + 1);
    }
  }
  int L, R, mid, res;
  long long cnt = 0;
  for (i = 1; i <= n; i++) {
    L = 1;
    R = r[i];
    while (L <= R) {
      mid = (L + R) >> 1;
      if (check(i, mid)) {
        L = mid + 1;
        res = mid;
      } else
        R = mid - 1;
    }
    cnt += res;
  }
  cout << cnt << endl;
  return 0;
}
