#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}
const int mod = 1e9 + 7;
const int maxn = 1e6 + 6;
bool a[maxn];
int b[maxn];
int c[maxn];
int I[maxn];
void solve() {
  int n, m, x;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    I[b[i]] = i;
  }
  a[b[1]] = true;
  for (int j = 2; j <= m; j++) {
    for (int i = 1; i <= n; i++) {
      cin >> c[i];
      if (c[i - 1] != b[I[c[i]] - 1]) a[c[i]] = true;
    }
  }
  long long res = 0;
  int col = 0;
  for (int i = 1; i <= n; i++) {
    if (a[b[i]]) {
      res += col * (col + 1LL) / 2;
      col = 0;
    }
    col++;
  }
  res += col * (col + 1LL) / 2;
  cout << res;
}
