#include <bits/stdc++.h>
using namespace std;
long long int mx(long long int a, long long int b) {
  if (a >= b) return a;
  return b;
}
long long int mn(long long int a, long long int b) {
  if (a < b) return a;
  return b;
}
long long int MOD(long long int x, long long int m) {
  if (m == 0) {
    cout << "WTF BRO ?  ---> :(";
    return -1;
  }
  if (x < 0)
    return MOD(x + m, m);
  else
    return x % m;
}
long long int pw(long long int x, long long int pow, long long int M = 0) {
  long long int res = 1;
  while (pow >= 1) {
    if (pow & 1) {
      res = MOD(res * x, M);
    }
    x = MOD(x * x, M);
    pow /= 2;
  }
  return res;
}
long long int n, m;
vector<pair<long long int, long long int> > s;
vector<map<long long int, long long int> > q(15);
vector<vector<long long int> > arr(15);
vector<vector<pair<long long int, long long int> > > sn(15);
int main() {
  scanf(" %lld", &n);
  scanf(" %lld", &m);
  for (long long int i = 0; i < m; i++) {
    for (long long int j = 0; j < n; j++) {
      long long int x;
      scanf(" %lld", &x);
      q[i][x] = j;
      arr[i].push_back(x);
    }
  }
  for (long long int i = 0; i < n; i++) {
    sn[0].push_back(make_pair(i, n - 1));
  }
  for (long long int i = 1; i < m; i++) {
    long long int fixer = 0;
    for (long long int j = 0; j < sn[i - 1].size(); j++) {
      pair<long long int, long long int> x = sn[i - 1][j];
      long long int sub_ind = q[i][arr[0][mx(x.first, fixer)]];
      for (long long int t = mx(x.first, fixer); t <= x.second;
           t++, sub_ind++) {
        if (arr[i][sub_ind] != arr[0][t]) {
          sn[i].push_back(make_pair(x.first, t - 1));
          fixer = t - 1;
          break;
        } else {
          if (sub_ind == n - 1 or t == x.second) {
            sn[i].push_back(make_pair(x.first, t));
            fixer = t;
            break;
          }
        }
      }
    }
  }
  long long int res = 0;
  for (long long int i = 0; i < sn[m - 1].size(); i++) {
    pair<long long int, long long int> x = sn[m - 1][i];
    res += x.second - x.first + 1;
  }
  printf("%lld", res);
  return 0;
}
