#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (!y) return x;
  return gcd(y, x % y);
}
long long expo(long long x, long long y, long long m) {
  long long res = 1LL;
  while (y) {
    if (y & 1) res = (res * x) % m;
    x = (x * x) % m;
    y /= 2;
  }
  return res;
}
class matrix {
 public:
  long long a[3][3];
  matrix() { memset(a, 0, sizeof a); }
  long long *operator[](long long i) { return a[i]; }
  matrix operator*(matrix b) {
    matrix c;
    long long i, j, k;
    for (i = 0; i < 3; i++)
      for (j = 0; j < 3; j++)
        for (k = 0; k < 3; k++)
          c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % 1000000007) % 1000000007;
    return c;
  }
};
matrix expo(matrix x, long long y) {
  matrix res;
  long long i;
  for (i = 0; i < 3; i++) res[i][i] = 1;
  while (y) {
    if (y & 1) res = (res * x);
    x = (x * x);
    y /= 2;
  }
  return res;
}
long long bits(long long x) {
  long long ans = 0;
  while (x) {
    if (x & 1) ans++;
    x >>= 1;
  }
  return ans;
}
long long mp[12][100005], a[12][100005], has[12][100004], P[100005],
    inv[100004];
long long H(long long i, long long l, long long r) {
  if (l < 1) return -1LL;
  long long ans =
      (((has[i][r] - has[i][l - 1] + 1000000007) % 1000000007) * inv[l - 1]) %
      1000000007;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m, i, j;
  P[0] = 1LL;
  inv[0] = expo(P[0], 1000000007 - 2, 1000000007);
  for (i = 1; i <= 100000; i++) {
    P[i] = (P[i - 1] * 44701) % 1000000007;
    inv[i] = expo(P[i], 1000000007 - 2, 1000000007);
  }
  cin >> n >> m;
  for (j = 1; j <= m; j++)
    for (i = 1; i <= n; i++) {
      cin >> a[j][i];
      mp[j][a[j][i]] = i;
    }
  for (i = 1; i <= m; i++)
    for (j = 1; j <= n; j++)
      has[i][j] = (has[i][j - 1] + (P[j] * a[i][j])) % 1000000007;
  long long ans = 0;
  for (i = 1; i <= n; i++) {
    long long l = 0;
    long long r = n - 1;
    long long curr = 0;
    while (l <= r) {
      long long mid = (l + r) / 2;
      long long ok = 1;
      long long fi = -2;
      for (j = 1; j <= m; j++) {
        long long x = H(j, mp[j][i] - mid, mp[j][i]);
        if (x == -1) {
          ok = 0;
          break;
        }
        if (fi == -2) fi = x;
        if (fi != x) {
          ok = 0;
          break;
        }
      }
      if (ok)
        curr = mid, l = mid + 1;
      else
        r = mid - 1;
    }
    ans += curr + 1;
  }
  cout << ans;
}
