#include <bits/stdc++.h>
using namespace std;
long long const mod = 1e9 + 7;
long long power(long long x, long long y, long long mod = 2e18) {
  long long ans = 1;
  x %= mod;
  while (y) {
    if (y & 1) ans = (x * ans) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
unordered_map<long long, long long> m1[10];
long long arr[10][100001];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  for (long long j = 0; j < m; j++)
    for (long long i = 0; i < n; i++) {
      cin >> arr[j][i];
      m1[j][arr[j][i]] = i;
    }
  long long ans = n;
  for (long long i = 0; i < n; i++) {
    long long j = 1;
    long long tmp1 = m;
    while (tmp1 == m && i + j < n) {
      tmp1 = 1;
      long long tmp = arr[0][i + j];
      for (long long k = 1; k < m; k++) {
        long long ind = m1[k][tmp];
        if (ind - m1[k][arr[0][i]] == j) {
          tmp1++;
        }
      }
      if (tmp1 == m) {
        j++;
      }
    }
    ans += j * (j - 1) / 2;
    i = i + j - 1;
  }
  cout << ans;
  return 0;
}
