#include <bits/stdc++.h>
using namespace std;
int n, m, arr[15][100000 + 5], cari[15][100000 + 5], nex[15], temp[15];
long long res;
deque<int> deq;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &arr[i][j]);
      cari[i][arr[i][j]] = j;
    }
    nex[i] = -1;
  }
  for (int j = 0; j < n; j++) {
    deq.push_back(arr[0][j]);
    bool flag = false;
    while (!flag) {
      flag = true;
      for (int i = 1; i < m; i++) {
        if (nex[i] == -1) {
          int curpos = cari[i][deq.back()];
          temp[i] = arr[i][curpos + 1];
        } else if (nex[i] != deq.back()) {
          flag = false;
          break;
        } else {
          int curpos = cari[i][nex[i]];
          temp[i] = arr[i][curpos + 1];
        }
      }
      if (flag) {
        res += deq.size();
        for (int i = 1; i < m; i++) {
          nex[i] = temp[i];
        }
      } else {
        deq.clear();
        deq.push_back(arr[0][j]);
        for (int i = 1; i < m; i++) nex[i] = -1;
      }
    }
  }
  printf("%lld\n", res);
}
