#include <bits/stdc++.h>
using namespace std;
int a[10][100100];
int id[10][100100];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      id[i][a[i][j]] = j;
    }
  long long cnt = 0;
  for (int l = 1; l <= n;) {
    int r = l + 1;
    for (; r <= n; r++) {
      int cur_v = a[0][r - 1];
      int next_v = a[0][r];
      bool all_good = true;
      for (int i = 1; i < m; i++) {
        int j = id[i][cur_v];
        if (j >= n || a[i][j + 1] != next_v) {
          all_good = false;
          break;
        }
      }
      if (!all_good) break;
    }
    long long k = r - l;
    cnt += k * (k + 1) / 2;
    l = r;
  }
  cout << cnt;
}
