#include <bits/stdc++.h>
using namespace std;
const long double epsylon = 1e-9;
const std::string PROGRAM_NAME = "ivo";
const int NUM_HASHES = 1;
class Comp {
 public:
  Comp(const vector<int>& v, int num_hashes = NUM_HASHES) {
    val = v;
    hash_count = num_hashes;
    for (int i = 0; i < num_hashes; i++) {
      if (Comp::pows_of_prime[i].size() == 0) {
        Comp::pows_of_prime[i].resize(max_word_size);
        Comp::pows_of_prime[i][0] = 1;
        for (int j = 1; j < max_word_size; j++)
          Comp::pows_of_prime[i][j] =
              Comp::pows_of_prime[i][j - 1] * hash_bases[i];
      }
    }
    n = (int)val.size();
    codes.resize(num_hashes);
    for (int j = 0; j < num_hashes; j++) {
      codes[j].resize(n + 1);
      codes[j][0] = 0;
      for (int i = 0; i < n; i++)
        codes[j][i + 1] = codes[j][i] * hash_bases[j] + val[i];
    }
  }
  vector<unsigned long long> getcode(int from, int len) {
    vector<unsigned long long> res;
    for (int j = 0; j < hash_count; j++) {
      int to = from + len - 1;
      res.push_back(codes[j][to + 1] -
                    codes[j][from] * Comp::pows_of_prime[j][to - from + 1]);
    }
    return res;
  }

 private:
  vector<vector<unsigned long long> > codes;
  vector<int> val;
  int hash_count;
  static const unsigned long long prime_for_compare = 10003;
  static const unsigned long long hash_bases[5];
  static const int max_word_size = 100001;
  static vector<vector<unsigned long long> > pows_of_prime;
  int n;
};
vector<vector<unsigned long long> > Comp::pows_of_prime =
    vector<vector<unsigned long long> >(5);
const unsigned long long Comp::hash_bases[5] = {1000203, 10003, 257, 29, 1023};
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > rev(m, vector<int>(n + 1));
  vector<vector<int> > a(m, vector<int>(n));
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%d", &a[i][j]);
      rev[i][a[i][j]] = j;
    }
  }
  vector<Comp> rk;
  for (int i = 0; i < m; ++i) {
    rk.push_back(Comp(a[i]));
  }
  long long total = 0;
  for (int i = 0; i < n; ++i) {
    int beg = 1;
    int end = n - i + 1;
    while (end - beg > 1) {
      int mid = (end + beg) / 2;
      vector<unsigned long long> kod = rk[0].getcode(i, mid);
      bool good = true;
      for (int j = 1; j < m; ++j) {
        int idx = rev[j][a[0][i]];
        if (idx + mid > n) {
          good = false;
          break;
        }
        if (kod != rk[j].getcode(idx, mid)) {
          good = false;
          break;
        }
      }
      if (good) {
        beg = mid;
      } else {
        end = mid;
      }
    }
    total += (long long)beg;
  }
  cout << total;
  return 0;
}
