#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
const long long INF = 1011111111;
const long long LLINF = 1000111000111000111LL;
const long double EPS = 1e-10;
const long double PI = 3.14159265358979323;
using namespace std;
long long powm(long long base, long long exp, long long mod = MOD) {
  long long ans = 1;
  while (exp) {
    if (exp & 1) ans = (ans * base) % mod;
    exp >>= 1, base = (base * base) % mod;
  }
  return ans;
}
const long long N = 100005;
long long a[15][N], mk[15], pos[N][15];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i < m + 1; i++)
    for (long long j = 1; j < n + 1; j++) cin >> a[i][j], pos[a[i][j]][i] = j;
  long long ans = 0, l = 1, r = 1, got;
  while (l <= n) {
    for (long long j = 1; j < m + 1; j++) mk[j] = pos[a[1][l]][j];
    r++, got = 1;
    while (r <= n) {
      bool flag = 1;
      for (long long j = 1; j < m + 1; j++) {
        mk[j]++;
        flag &= a[j][mk[j]] == a[1][r];
      }
      if (!flag) break;
      r++;
    }
    ans += ((r - l) * (r - l + 1)) / 2;
    l = r;
  }
  cout << ans;
  return 0;
}
