#include <bits/stdc++.h>
const int N = 100007, inf = 0x3f3f3f3f;
using namespace std;
int M[] = {(int)1e9 + 7, (int)1e9 + 9};
const int b1 = 1e5 + 7, b2 = 1e5 + 9;
int v[11][N];
long long pot[2][2][N];
long long p[2][2][11][N];
int n, m;
void calc() {
  pot[0][0][0] = 1;
  pot[1][0][0] = 1;
  for (int i = 1; i < N; ++i) {
    pot[0][0][i] = pot[0][0][i - 1] * b1 % M[0];
    pot[1][0][i] = pot[1][0][i - 1] * b2 % M[0];
  }
  for (int i = 0; i < m; ++i) {
    p[0][0][i][0] = v[i][0];
    p[1][0][i][0] = v[i][0];
    for (int j = 1; j < n; ++j) {
      p[0][0][i][j] = (p[0][0][i][j - 1] * b1 % M[0] + v[i][j]) % M[0];
      p[1][0][i][j] = (p[1][0][i][j - 1] * b2 % M[0] + v[i][j]) % M[0];
    }
  }
}
int _hash(int t, int i, int j, int b, int mod) {
  if (i == 0) return p[b][mod][t][j];
  return (p[b][mod][t][j] -
          pot[b][mod][j - i + 1] * p[b][mod][t][i - 1] % M[mod] + M[mod]) %
         M[mod];
}
pair<int, int> turciehfera(int t, int i, int j) {
  return pair<int, int>(_hash(t, i, j, 0, 0), _hash(t, i, j, 1, 0));
}
int start[11][N];
inline int check(int fir, int sz, pair<int, int> x) {
  for (int k = 0; k < m; ++k) {
    int l = start[k][fir];
    int r = l + sz - 1;
    if (r >= n or turciehfera(k, l, r) != x) return 0;
  }
  return 1;
}
int main() {
  int i, j;
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      scanf("%d", &v[i][j]);
      start[i][v[i][j]] = j;
    }
  }
  calc();
  long long tot = 0;
  int l, m, r;
  pair<int, int> x;
  for (int i = 0; i < n; ++i) {
    l = 0, r = n - i;
    while (l < r) {
      m = (l + r + 1) / 2;
      x = turciehfera(0, i, i + m - 1);
      if (check(v[0][i], m, x) == 0)
        r = m - 1;
      else
        l = m;
    }
    tot += l;
  }
  printf("%lld\n", tot);
}
