#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> P(m);
  vector<vector<int>> Q(m);
  for (int i = 0; i < m; i++) {
    Q[i].resize(n + 1);
    for (int j = 0; j < n; j++) {
      int x;
      cin >> x;
      P[i].push_back(x);
      Q[i][x] = j;
    }
  }
  long long ans = 0;
  vector<int> l(m), r(m);
  while (l[0] < n && r[0] < n) {
    if (l[0] == r[0]) {
      for (int i = 1; i < m; i++) {
        l[i] = Q[i][P[0][l[0]]];
        r[i] = Q[i][P[0][r[0]]];
      }
      ans++;
      for (int i = 0; i < m; i++) r[i]++;
      continue;
    }
    int cur = P[0][r[0]];
    bool flag = true;
    for (int i = 1; i < m; i++) {
      if (r[i] >= n || P[i][r[i]] != cur) {
        flag = false;
        break;
      }
    }
    if (flag) {
      ans += r[0] - l[0] + 1;
      for (int i = 0; i < m; i++) r[i]++;
    } else {
      l[0] = r[0];
    }
  }
  cout << ans << endl;
  return 0;
}
