#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1e9 + 7;
long long int INF = 1e18;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k = 0, m = 0, n, p = 0, l, r, ans = 0, cnt = 0,
                      mini = INF, maxi = INF, x, y, a, b, c, low, mid, high, h,
                      q, k1, l1, t;
  cin >> n >> m;
  long long int A[m][n + 1];
  long long int B[m][n + 1];
  for (j = 0; j < m; j++) {
    for (i = 1; i <= n; i++) {
      cin >> k;
      A[j][i] = k;
      B[j][k] = i;
    }
  }
  ans = n;
  h = 1;
  for (i = 1; i < n; i++) {
    j = 0;
    l = A[j][i];
    r = A[j][i + 1];
    p = 0;
    for (j = 1; j < m; j++) {
      if ((B[j][r] - B[j][l]) != 1) {
        h = 1;
        p = 1;
        break;
      }
    }
    if (p == 0) {
      h++;
      ans = ans + (h - 1);
    }
  }
  cout << ans << endl;
}
