#include <bits/stdc++.h>
using namespace std;
using VS = vector<string>;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VL = vector<LL>;
using VVL = vector<VL>;
const int INF = 1e9;
const LL LINF = 1e16;
const LL MOD = 1000000007;
const double PI = acos(-1.0);
int DX[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int DY[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int a[10][100005];
int pos[10][100005];
int N, M;
using uLL = unsigned long long;
const uLL _base = 1e9 + 7;
uLL hashed[10][100010];
uLL power[100010];
struct RollingHash {
  vector<uLL> hashed, power;
  uLL get(int l, int r) { return ((hashed[r] - hashed[l] * power[r - l])); }
  uLL connect(int h1, int h2, int h2len) { return (h1 * power[h2len] + h2); }
};
bool ok(int num, int sz) {
  sz++;
  if (sz == 1) return true;
  int L = pos[0][num];
  int R = pos[0][num] + sz;
  if (R > N) return false;
  uLL Hash = ((hashed[0][R] - hashed[0][L] * power[R - L]));
  ;
  for (int(i) = (1); (i) < (M); (i)++) {
    int LL = pos[i][num];
    int RR = pos[i][num] + sz;
    ;
    if (RR > N) return false;
    uLL Hash2 = ((hashed[i][RR] - hashed[i][LL] * power[RR - LL]));
    if (Hash != Hash2) return false;
  }
  return true;
}
int main() {
  power[0] = 1;
  for (int i = 0; i < 100005; i++) {
    power[i + 1] = power[i] * _base;
  }
  scanf("%d%d", &N, &M);
  for (int(i) = (0); (i) < (M); (i)++) {
    for (int(j) = (0); (j) < (N); (j)++) {
      scanf("%d", &a[i][j]);
      a[i][j]--;
      pos[i][a[i][j]] = j;
    }
    for (int(j) = (0); (j) < (N); (j)++) {
      hashed[i][j + 1] = (hashed[i][j] + a[i][j]) * _base;
    }
  }
  LL ans = 0;
  for (int l = 0, r = 0; l < N; l++) {
    while (r < N && ok(a[0][l], r - l)) {
      r++;
    };
    ans += r - l;
  }
  cout << ans << "\n";
  return 0;
}
