#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int a[m][n];
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) cin >> a[i][j];
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) --a[i][j];
  int mp[n];
  for (int i = 0; i < n; i++) {
    mp[a[0][i]] = i;
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) a[i][j] = mp[a[i][j]];
  }
  int pre[m][n];
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) pre[i][j] = 1;
  for (int i = 0; i < m; i++) {
    int j = 0;
    while (j < n) {
      int j1 = j + 1;
      while (j1 < n && a[i][j1] == a[i][j1 - 1] + 1) {
        assert(a[i][j] < n);
        pre[i][a[i][j]]++;
        j1++;
      }
      j = j1;
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (pre[i][j] > 1) {
        assert(j < n - 1);
        pre[i][j + 1] = pre[i][j] - 1;
      }
    }
  }
  long long ans = 0LL;
  int i = 0;
  while (i < n) {
    int mn = pre[0][i];
    for (int j = 0; j < m; j++) {
      mn = min(mn, pre[j][i]);
    }
    ans += 1LL * mn * (mn + 1) / 2;
    i += mn;
  }
  cout << ans;
  return 0;
}
