#include <bits/stdc++.h>
using namespace std;
int _toggle(int N, int pos) { return N = N ^ (1 << pos); }
int _set(int N, int pos) { return N = N | (1 << pos); }
int _reset(int N, int pos) { return N = N & ~(1 << pos); }
bool _check(int N, int pos) { return (bool)(N & (1 << pos)); }
bool _upper(char a) { return a >= 'A' && a <= 'Z'; }
bool _lower(char a) { return a >= 'a' && a <= 'z'; }
bool _digit(char a) { return a >= '0' && a <= '9'; }
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
int a[12][100010], pl[12][100010];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) cin >> a[i][j], pl[i][a[i][j]] = j;
  }
  long long ans = 0, cnt = 0;
  for (int k = 1; k <= n; k++) {
    if (k == 1) {
      cnt++;
      continue;
    }
    int x = a[1][k], y = a[1][k - 1], f = 0;
    for (int i = 2; i <= m; i++) {
      if (pl[i][x] - pl[i][y] != 1) f = 1;
    }
    if (f) {
      ans += 1LL * cnt * (cnt + 1) / 2;
      cnt = 1;
    } else
      cnt++;
  }
  ans += 1LL * cnt * (cnt + 1) / 2;
  cout << ans << '\n';
}
