#include <bits/stdc++.h>
using namespace std;
const int MAXN = 112345;
const int MAXINT = 2147483098;
const long long MAXLL = 9223372036854775258LL;
long long a[15][MAXN];
long long pf[15][MAXN];
long long pos[15][MAXN];
long long n, m;
int uk[MAXN];
long long sum(long long l, long long r) {
  long long big_sum = r * (r + 1) / 2;
  long long Minus = l * (l - 1) / 2;
  return big_sum - Minus;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> a[i][j];
      pf[i][j] = pf[i][j - 1] + a[i][j];
      pos[i][a[i][j]] = j;
    }
  }
  long long ans = 0;
  long long R = 1;
  for (long long L = 1; L <= n; ++L) {
    while (R < L) ++R;
    while (1) {
      bool flag = 0;
      if (R == n) break;
      for (int i = 2; i <= m; ++i) {
        int nxt = pos[i][a[1][R + 1]];
        int prev = pos[i][a[1][R]];
        if (nxt != prev + 1) {
          flag = 1;
          break;
        }
      }
      if (flag) break;
      R++;
    }
    long long have = R - L + 1;
    ans += have;
  }
  cout << ans << '\n';
  return 0;
}
