#include<cstdio>
#include<bitset>
using namespace std;
const int N=4e4+10;
int n,k,a[N],inv[N];
bitset<N> f[N];//f[i]表示满足a[j]<a[i](j<i)的j的bitset
int main()
{
freopen("partial_order_plus.in","r",stdin);
freopen("partial_order_plus.out","w",stdout);
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++) f[i]=f[i-1],f[i][i-1]=1;//做出来上三角,排除顺序的干扰
while (k--){
for (int i=1;i<=n;i++) scanf("%d",&a[i]),inv[a[i]]=i;
bitset<N> now;
for (int i=1;i<=n;i++) f[inv[i]]&=now,now[inv[i]]=1;
}
int ans=0;
for (int i=1;i<=n;i++) ans+=f[i].count();
printf("%d\n",ans);
return 0;
}