#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 20, M = 12;
int mod, n, m, a[M][N], idx[M][N];
bool ok(int l, int r) {
  int first = a[0][l], last = a[0][r];
  for (int i = 1; i < m; i++) {
    int start = idx[i][first];
    if (start + r - l > n || a[i][start + r - l] != last) {
      return false;
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      idx[i][a[i][j]] = j;
    }
  }
  long long ans = 0;
  for (int i = 1, j = 1; i <= n; i++) {
    while (j <= n and ok(i, j)) j++;
    ans += j - i;
  }
  cout << ans;
}
