#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::string;
using std::vector;
int n, m;
int a[12][1 << 18], b[12][1 << 18];
int pos[12];
int solve() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%d", &a[i][j]);
      b[i][a[i][j]] = j;
    }
  }
  long long ans = 0;
  int d = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (i == n - 1) {
      int x = a[0][i];
      for (int k = 0; k < m; ++k) {
        pos[k] = b[k][x];
      }
      d = 1;
      ans += d;
    } else {
      int p = i;
      int x = a[0][i];
      bool yes = true;
      for (int k = 0; k < m; ++k) {
        yes = yes && pos[k] > 0 && a[k][pos[k] - 1] == x;
      }
      if (!yes) {
        d = 1;
        ans += d;
        for (int k = 0; k < m; ++k) {
          pos[k] = b[k][x];
        }
        continue;
      }
      for (int k = 0; k < m; ++k) {
        --pos[k];
      }
      ++d;
      ans += d;
    }
  }
  cout << ans;
  return 0;
}
int main(int argc, char* argv[]) {
  ::std::ios::sync_with_stdio(false);
  ::std::cin.tie(0);
  ::std::cout.tie(0);
  int test_count = 1;
  while (test_count--) solve();
  return 0;
}
