#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
const int mod = (1e9) + 7;
const double eps = 1e-10;
const int siz = 1e5 + 5, siz2 = 18, siz3 = 5032108;
int n, m, arr[11][siz], ind[11][siz];
long long ans;
vector<int> ps;
int check(int st) {
  while (true) {
    for (int i = 0; i < m; i++) {
      ps[i]++;
    }
    if (ps[0] == n) {
      return ps[0] - st;
    }
    for (int i = 1; i < m; i++) {
      if (ps[i] == n || arr[i][ps[i]] != arr[i - 1][ps[i - 1]]) {
        return ps[0] - st;
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &arr[i][j]);
      ind[i][arr[i][j]] = j;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ps.push_back(ind[j][arr[0][i]]);
    }
    int v = check(i);
    int fv = v;
    for (int j = i; j < i + fv; j++) {
      ans += v;
      v--;
    }
    i += fv - 1;
    ps.clear();
  }
  printf("%I64d\n", ans);
  return 0;
}
