#include <bits/stdc++.h>
using namespace std;
long long t, n, m, k, minn, maxx = -1, ans = 0, sum = 0, d, a[100005][11],
                            b[100005][11], tl, cnt = 1, pos[100005], le[100005],
                            want;
string s;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  for (int i = 0; i < 100002; i++) {
    pos[i] = 0;
    le[i] = 0;
  }
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[j][i];
      b[a[j][i]][i] = j;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (pos[i] == 0) {
      tl = 0;
      for (int j = 0;; j++) {
        d = 1;
        for (int k = 0; k < m - 1; k++) {
          if (a[b[i][k] + j][k] != a[b[i][k + 1] + j][k + 1] ||
              (b[i][k] + j > n || b[i][k + 1] + j > n)) {
            d = 0;
            break;
          }
        }
        if (d && b[i][0] + j <= n) {
          if (pos[a[b[i][0] + j][0]] > 0) {
            for (int k = 0; k < j; k++) {
              pos[a[b[i][0] + k][0]] = pos[a[b[i][0] + j][0]];
            }
            le[pos[a[b[i][0] + j][0]]] += j;
            break;
          }
        } else {
          for (int k = 0; k < j; k++) {
            pos[a[b[i][0] + k][0]] = cnt;
          }
          le[cnt] = j;
          cnt++;
          break;
        }
      }
    }
  }
  for (int i = 1; i < cnt; i++) {
    ans += le[i] * (le[i] + 1) / 2;
  }
  cout << ans;
}
