#include <bits/stdc++.h>
using namespace std;
int p1 = 37, p2 = 41, p3 = 47;
int mod1 = 99107213, mod2 = 98837671;
void fill_h(const vector<int> &v, vector<long long> &h, int mod, int p, int n) {
  for (int i = 1; i <= n; ++i) {
    h[i] = ((h[i - 1] * p) + v[i - 1]) % mod;
  }
}
long long get_h(const vector<long long> &h, const vector<long long> &powr,
                int l, int r, int mod) {
  long long big = h[r + 1], small = h[l];
  return (big + mod - (small * powr[r - l + 1]) % mod) % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(m, vector<int>(n));
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> v[i][j];
    }
  }
  vector<vector<long long>> h1(m, vector<long long>(n + 1));
  for (int i = 0; i < m; ++i) {
    fill_h(v[i], h1[i], mod1, p1, n);
  }
  vector<long long> pow1(n + 1, 1);
  for (long long i = 1; i <= n; ++i) {
    pow1[i] = (pow1[i - 1] * p1) % mod1;
  }
  long long ans = 0;
  vector<vector<int>> pos(m, vector<int>(n + 1));
  for (long long i = 0; i < m; ++i) {
    for (long long j = 0; j < n; ++j) {
      pos[i][v[i][j] - 1] = j;
    }
  }
  for (int el = 0; el < n; ++el) {
    int l = 0, r = n;
    for (int i = 0; i < m; ++i) {
      r = min(r, n - pos[i][el]);
    }
    while (l + 1 < r) {
      int mid = (l + r) / 2;
      long long c1 = get_h(h1[0], pow1, pos[0][el], pos[0][el] + mid, mod1);
      int q = 1;
      for (int j = 1; j < m; ++j) {
        if (get_h(h1[j], pow1, pos[j][el], pos[j][el] + mid, mod1) != c1) {
          q = 0;
          break;
        }
      }
      if (q) {
        l = mid;
      } else {
        r = mid;
      }
    }
    ans += (l + 1);
  }
  cout << ans;
  return 0;
}
