#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 11;
int pos[N][M], p[N][M], prevPos[M];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int j = 1; j <= m; ++j) {
    for (int i = 1; i <= n; ++i) {
      cin >> p[i][j];
      pos[p[i][j]][j] = i;
    }
  }
  int l = 1, r;
  long long ans = 0;
  while (l <= n) {
    r = l;
    for (int j = 1; j <= m; ++j) {
      prevPos[j] = pos[p[l][1]][j];
    }
    while (r + 1 <= n) {
      bool isGood = true;
      for (int j = 1; j <= m; ++j) {
        if (prevPos[j] + 1 > n || p[prevPos[j] + 1][j] != p[r + 1][1]) {
          isGood = false;
          break;
        }
      }
      if (isGood) {
        for (int j = 1; j <= m; ++j) {
          prevPos[j] += 1;
        }
        r += 1;
      } else {
        break;
      }
    }
    ans += (r - l + 1) * 1ll * (r - l + 2) / 2;
    l = r + 1;
  }
  cout << ans << endl;
  return 0;
}
