#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int modInverse(long long int n, long long int p) {
  return power(n, p - 2, p);
}
struct point {
  long long int x, y;
};
vector<long long int> ans;
vector<vector<long long int>> m1(11, vector<long long int>(100001, 0));
void solve(vector<vector<long long int>> &arr, long long int num,
           long long int n, long long int m) {
  vector<long long int> ind(m, 0);
  for (long long int i = 0; i < m; i++) {
    ind[i] = m1[i][num];
  }
  long long int thresh = 0;
  vector<long long int> seq;
  for (long long int j = 0; j < n; j++) {
    set<long long int> s1;
    for (long long int i = 0; i < m; i++) {
      if (ind[i] + j >= arr[i].size()) {
        goto zee;
      }
      s1.insert(arr[i][ind[i] + j]);
    }
    if (s1.size() == 1) {
      if (ans[*s1.begin()] != 0) {
        thresh = ans[*s1.begin()];
        goto zee;
      }
      seq.push_back(*s1.begin());
    } else {
      break;
    }
  }
zee:;
  long long int sz = seq.size();
  for (int i = 0; i < sz; i++) {
    ans[seq[i]] = sz - i + thresh;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  ans.assign(n + 1, 0);
  vector<vector<long long int>> arr(m, vector<long long int>(n, 0));
  for (long long int i = 0; i < m; i++) {
    for (long long int j = 0; j < n; j++) {
      cin >> arr[i][j];
      m1[i][arr[i][j]] = j;
    }
  }
  for (long long int num = 1; num < n + 1; num++) {
    if (ans[num] == 0) {
      solve(arr, num, n, m);
    }
  }
  long long int fans = 0;
  for (long long int i = 1; i < n + 1; i++) {
    fans += ans[i];
  }
  cout << fans << "\n";
}
