// Arup Guha
// 12/19/2019
// Solution to 2019 December USACO Bronze Problem: Cow Gymnastics

import java.util.*;
import java.io.*;

public class gymnastics {

	public static void main(String[] args) throws Exception {
		
		// Read in the basic graph parameters.
		Scanner stdin = new Scanner(new File("gymnastics.in"));
		int nPrac = stdin.nextInt();
		int nCows = stdin.nextInt();
		
		// Read scores, but just store each cow's rank.
		int[][] ranks = new int[nPrac][nCows];
		for (int i=0; i<nPrac; i++) {
			for (int j=0; j<nCows; j++) {
				int tmp = stdin.nextInt();
				ranks[i][tmp-1] = j;
			}
		}
		
		int res = 0;
		
		// Go through all pairs.
		for (int i=0; i<nCows; i++) {
			for (int j=i+1; j<nCows; j++) {
			
				// See how many times cow i beat cow j.
				int iBeatj = 0;
				for (int k=0; k<nPrac; k++)
					if (ranks[k][i] < ranks[k][j])
						iBeatj++;
						
				// 0 or all is "consistent"
				if (iBeatj == 0 || iBeatj == nPrac)
					res++;
			}
		}
		
		// Output to file.
		PrintWriter out = new PrintWriter(new FileWriter("gymnastics.out"));
		out.println(res);
		out.close();		
		stdin.close();
	}
}