import java.util.Scanner;
class Main {
static int answer = 0;
static int[][] student;
public static void main(String args[]) throws Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = in.nextInt();
}
}
int cnt = 0 ;
//i 번째 학생
for (int i = 0 ; i < n ; ++i) {
//j 번째 학생
for (int j = 0 ; j < n ; ++j) {
boolean flag = true;
//자신과는 비교하지 않음
if( i==j) continue;
//k번째 테스트
for (int k = 0 ; k < m ; ++k) {
//맨토의 등수
int mentor = Integer.MAX_VALUE;
//맨티의 등수
int mentee = Integer.MAX_VALUE;
//h : grade
for (int h = 0 ; h < n; ++h) {
if( arr[k][h] == i+1 )
mentor = h;
if (arr[k][h] == j+1)
mentee = h;
}
//맨티가 더 시험을 잘본 경우.
if( mentor > mentee) {
//System.out.println(String.format("[k:%d] [%d, %d] [%d,%d]",k+1, i+1 ,mentor+1,j+1, mentee+1 ));
flag=false;
break;
}
}
if( flag ) {
cnt++;
}
}
}
System.out.println(cnt);
}
}
ex)
input
4 3
3 4 1 2
4 3 2 1
3 1 4 2
output
3